Skip to content

Instantly share code, notes, and snippets.

@tspannhw
Forked from alexlopes/kafka_python_sasl_scram.py
Created February 8, 2023 21:10
Show Gist options
  • Select an option

  • Save tspannhw/f9e25ad8268184b087e301c82f3cdce6 to your computer and use it in GitHub Desktop.

Select an option

Save tspannhw/f9e25ad8268184b087e301c82f3cdce6 to your computer and use it in GitHub Desktop.
Kafka Python with SASL/SCRAM Authentication Example
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment