Last active
December 25, 2024 10:37
-
-
Save alexlopes/72fea4e4da623ef8f60a800d6a962f2f to your computer and use it in GitHub Desktop.
Kafka Python with SASL/SCRAM Authentication Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from kafka import KafkaProducer, KafkaConsumer | |
BOOTSTRAP_SERVERS=os.gentenv("KAFKA_BOOTSTRAP_SERVERS").split(",") | |
TOPIC_NAME="the-topic" | |
SASL_USERNAME=os.gentenv("KAFKA_SASL_USERNAME") | |
SASL_PASSWORD=os.gentenv("KAFKA_SASL_PASSWORD") | |
def consume(): | |
consumer = KafkaConsumer(TOPIC_NAME, security_protocol="SASL_SSL", sasl_mechanism="SCRAM-SHA-512", sasl_plain_username=SASL_USERNAME, sasl_plain_password=SASL_PASSWORD, bootstrap_servers=BOOTSTRAP_SERVERS) | |
for msg in consumer: | |
print (msg) | |
def produce(): | |
producer = KafkaProducer(security_protocol="SASL_SSL", sasl_mechanism="SCRAM-SHA-512", sasl_plain_username=SASL_USERNAME, sasl_plain_password=SASL_PASSWORD, bootstrap_servers=BOOTSTRAP_SERVERS) | |
producer.send(TOPIC_NAME, b'some_message_bytes') |
Hi @even986025158, what is your kafka-python lib version? I found this thread suggestion upgrade the version (to accept other mechanisms)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
but when I was run,it is happen the error 'AssertionError: sasl_mechanism must be in PLAIN, GSSAPI' ;how can I solve this problem