- Documentation
Cloud Native Message Engine
BMQ Instance User Guide
SDK reference
Python SDK
Python SDK
This topic describes how to use the Python SDK to connect to the cloud-native messaging engine BMQ and send and receive messages.
- (Optional) To authenticate using a SASL username and password, you must first create a user and obtain the password. For detailed instructions, see Create SASL user.
pip install kafka-python
logger = logging.getLogger('kafka')
logger.addHandler(logging.StreamHandler(sys.stdout))
logger.setLevel(logging.DEBUG)
Create and write producer.py to send messages.
When connecting to the BMQ instance using the endpoint address with the PLAINTEXT protocol, authentication is not required.
from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers='your broker list',
result = producer.send('your topic', b'some_message_bytes').get()
print("send message: partition " + str(result.partition) + " offset " + str(result.offset))
Authenticate using a SASL username and password. You must obtain the username and password of an authorized user. For instructions, see View SASL user password. from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers="your broker list",
security_protocol="SASL_PLAINTEXT",
sasl_plain_username="User name",
sasl_plain_password="Password",
result = producer.send('your topic', b'some_message_bytes').get()
print("send message: partition " + str(result.partition) + " offset " + str(result.offset))
Authenticate using a SASL username and password. You must obtain the username and password of an authorized user. For instructions, see View SASL user password. from kafka import KafkaProducer
producer = KafkaProducer(
bootstrap_servers="your broker list",
security_protocol="SASL_SSL",
sasl_plain_username="User name",
sasl_plain_password="Password",
ssl_check_hostname=False,
result = producer.send('your topic', b'some_message_bytes').get()
print("send message: partition " + str(result.partition) + " offset " + str(result.offset))
Create and write consumer.py to receive messages.
When connecting to the BMQ instance using the endpoint address with the PLAINTEXT protocol, authentication is not required.
from kafka import KafkaConsumer
consumer = KafkaConsumer('your topic', bootstrap_servers="your broker list", group_id="your consumer group",api_version=(0, 10, 2))
Authenticate using a SASL username and password. You must obtain the username and password of an authorized user. For instructions, see View SASL user password. from kafka import KafkaConsumer
consumer = KafkaConsumer(
bootstrap_servers="your broker list",
group_id="your consumer group",
security_protocol="SASL_PLAINTEXT",
sasl_plain_username="User name",
sasl_plain_password="Password",
Authenticate using a SASL username and password. You must obtain the username and password of an authorized user. For instructions, see View SASL user password. from kafka import KafkaConsumer
consumer = KafkaConsumer(
bootstrap_servers="your broker list",
group_id="your consumer group",
security_protocol="SASL_SSL",
sasl_plain_username="User name",
sasl_plain_password="Password",
ssl_check_hostname=False,
Last updated: 2026.01.26 15:46:58