tencent cloud

SASL_PLAINTEXT Access in the Public Network
Last updated:2026-01-05 15:16:58
SASL_PLAINTEXT Access in the Public Network
Last updated: 2026-01-05 15:16:58

Scenarios

This document uses the Python client as an example to describe how to access TDMQ for CKafka (CKafka) in the public network by using the SASL_PLAINTEXT method and send and receive messages.

Prerequisites

Operation Steps

Step 1: Preparations

1. Create an access point.
1.1 On the Instance List page, click the target instance ID to go to the instance details page.
1.2 Choose Basic Info > Access Mode, and click Add a routing policy. In the pop-up window, choose Routing Type: Public Network Domain Name Access > Access Method: SASL_PLAINTEXT.

2. Create a role. Choose ACL Policy Management > User Management. On the displayed page, create a role and set the password.

3. Create a Topic. On the Topic List page in the console, create a topic (see Creating a Topic).
4. Configure the ACL policy.
Configure the topic read/write permissions for the created role by seeing Configuring Topic Read/Write Permissions.
5. Add Python dependency libraries.
Run the following command to perform the installation:
pip install kafka-python
Note:
If the error message "ModuleNotFoundError: No module named 'kafka.vendor.six.moves'" is displayed during service, please check whether your Python version is 3.12 or later. You can try uninstalling kafka-python and installing kafka-python-ng:
pip uninstall kafka-python
pip install kafka-python-ng

Step 2: Producing Messages

1. Modify the configuration parameters in the message production program producer.py.
producer = KafkaProducer(
bootstrap_servers = ['xx.xx.xx.xx:port'],
api_version = (1, 1),

#
# SASL_PLAINTEXT access in the public network.
#
security_protocol = "SASL_PLAINTEXT",
sasl_mechanism = "PLAIN",
sasl_plain_username = "instanceId#username",
sasl_plain_password = "password",
)

message = "Hello World! Hello Ckafka!"
msg = json.dumps(message, ensure_ascii=False).encode()
producer.send('topic_name', value = msg)
print("produce message " + message + " success.")
producer.close()
Parameter
Description
bootstrap_servers
Access network. On the Basic Info page of the instance in the console, select the Access Mode module and copy the network information from the Network column.
sasl_plain_username
Username, in the format of instance ID + # + username. The instance ID can be obtained from the basic information on the instance details page in the CKafka console. Choose ACL Policy Management > User Management to create a user and set the username.
sasl_plain_password
User password. On the instance details page in the CKafka console, choose ACL Policy Management > User Management to create a user and set the password.
topic_name
Topic name. Copy the name on the Topic List page in the console.
2. Compile and run producer.py.
3. View the running results.


4. On the Topic List page in the CKafka console, select the target topic, and choose More > Message Query to view the message just sent.

Step 3: Consuming Messages

1. Modify the configuration parameters in the consumption message program consumer.py.
consumer = KafkaConsumer(
'topic_name',
group_id = "group_id",
bootstrap_servers = ['xx.xx.xx.xx:port'],
api_version = (1,1),

#
# SASL_PLAINTEXT access in the public network.
#
security_protocol = "SASL_PLAINTEXT",
sasl_mechanism = 'PLAIN',
sasl_plain_username = "instanceId#username",
sasl_plain_password = "password",
)

for message in consumer:
print ("Topic:[%s] Partition:[%d] Offset:[%d] Value:[%s]" %
(message.topic, message.partition, message.offset, message.value))
Parameter
Description
bootstrap_servers
Access network. On the Basic Info page of the instance in the console, select the Access Mode module and copy the network information from the Network column.
group_id
Consumer group ID. Define the group ID according to business requirements.
sasl_plain_username
Username, in the format of instance ID + # + username. The instance ID can be obtained from the basic information on the instance details page in the CKafka console. Choose ACL Policy Management > User Management to create a user and set the username.
sasl_plain_password
User password. On the instance details page in the CKafka console, choose ACL Policy Management > User Management to create a user and set the password.
topic_name
Topic name. Copy the name on the Topic List page in the console.
2. Compile and run consumer.py.
3. View the running results.


4. On the Consumer Group page in the CKafka console, select the target consumer group name, enter the topic name in the Topic Name area, and click View Details to view consumption details.

Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback