- Documentation
Message Queue for Kafka
SDK Reference
Apache Kafka SDK
Python client SDK
SASL_SSL access point (SCRAM mechanism) message production and consumption
Python client SDK
SASL_SSL access point (SCRAM mechanism) message production and consumption
SASL_SSL access point (SCRAM mechanism) message production and consumption
This topic uses the Python client as an example to describe how to connect to Message Queue for Kafka using the SCRAM mechanism through SASL_SSL endpoints in VPC or public network environments, and send and receive messages.
Prerequisites
Preparation work has been completed. For detailed explanations, refer to Preparations. 1. Add a configuration file
Create the Message Queue for Kafka configuration file config.json. When connecting to a SASL access point using the SCRAM mechanism, the following is an example configuration file. For detailed explanations of the configuration file fields, see SDK configuration description. tip
- Fill in the related parameters according to the comments and remove the comments.
- Under SCRAM mechanism, use a SCRAM user with corresponding Topic access rights for SASL authentication. For how to obtain the username and password, refer to 2 Collect Connection Information.
"bootstrap.servers": "xxxxx",
"security.protocol": "SASL_SSL",
"ssl.endpoint.identification.algorithm": "none",
"mechanism": "SCRAM-SHA-256",
2 Send messages
Implementation method
- Create the message sending program producer.py.
- Compile and run producer.py to send messages.
- Check the running results.
- The running result example is as follows.
-
tip
Message queue for Kafka provides an example project for quick integration. After downloading and extracting the demo, you can directly execute the following commands to send and consume messages. python3 {DemoPath}/bytedance_kafka.py
Example code
The following example code demonstrates how to produce messages using the SCRAM mechanism via a SASL_SSL access point. You can also refer to the example file {DemoPath}/client/producer.py to implement the relevant business logic.
from confluent_kafka import Producer
def callback(err, meta):
py:function:: callback(err, meta)
Handle the result of message delivery.
:param confluent_kafka.KafkaError err: error if delivery is failed
:param confluent_kafka.Message meta: message metadata if delivery is success
print('[INFO] Delivered message to topic {} [{}] at offset {}'
.format(meta.topic(), meta.partition(), meta.offset()))
print('[ERROR] Delivery failed: {}'.format(err))
p = Producer(conf.producer_conf())
p.produce(conf.topic(), 'demon message {}'.format(i).encode('utf-8'), on_delivery=callback)
3 consume messages
Implementation method
- Create a Consumer subscription message program consumer.py.
- Compile and run consumer.py to consume messages.
- Check the running results.
Example code
The following example code demonstrates how to consume messages using the SCRAM mechanism via a SASL_SSL endpoint. You can also refer to the example file {DemoPath}/client/consumer.py to implement the relevant business logic.
from confluent_kafka import Consumer
def handle_message(msg):
py:function:: handle_message(msg)
Handle consumed message.
:param confluent_kafka.Message msg: consumed message meta.
print('[INFO] Consumed a message from topic {} [{}] at offset {}. value: {}'.
format(msg.topic(), msg.partition(), msg.offset(), msg.value().decode('utf-8')))
c = Consumer(conf.consumer_conf())
c.subscribe([conf.topic()])
msg = c.poll(timeout=1.0)
print('[ERROR] Consume msg failed, {}'.format(msg.error()))
c.commit(asynchronous=False)
Last updated: 2026.04.29 18:34:22