如何连接 Kafka python 以接受 jaas 的用户名和密码,就像在 Java 中所做的那样?

How to do I connect Kafka python to accept username and password for jaas like it's done in Java?

使用现有的工作 Java 示例,我正在尝试编写一个 python 相当于使用 python-kafka 和 confluent_kafka 库的生产者。如何使用下面 Java 中的信息在 python 中配置 sasl.jass.config?

import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;

...
Properties props = new Properties();
...
props.put("sasl.jaas.config", "org.apache.kafka.common.security.scram.ScramLoginModule required username=\"<Kafka_Username>\" password=\"<Kafka_Password>\";");

Producer<String, String> producer = new KafkaProducer<>(props);

这对我有用

from confluent_kafka import Producer

SECURITY_PROTOCOL = "SASL_SSL"
SASL_MECHANISM = "PLAIN"

conf = {
 'bootstrap.servers': SERVERS,
 'sasl.mechanisms': SASL_MECHANISM,
 'security.protocol': SECURITY_PROTOCOL,
 'sasl.username': SASL_USERNAME,
 'sasl.password': SASL_PASSWORD,
 ...

 }
 consumer = Producer(conf)