【坑】Python通过kerberos安全认证操作kafka

如何通过Kerberos认证.

1.安装Kerberos客户端
CentOS:

2.拷贝Kerberos配置文件
conf目录下krb5.conf和kafka.keytab和jaas.conf拷贝到客户端机器的etc目录, 同时,krb5.conf中的kdc集群主机名和IP配置到客户端机器hosts配置文件中

3.Kinit客户端通过kerberos认证
获取Principal

4.安装python-gssapi

遇到的问题,如下:
a.在linux中执行wget命令提示 -bash: wget: command not found 解决方法

b.报错:bash: pip: command not found

3.pip安装出现Command “python setup.py egg_info” failed with error code 1 的解决方法

Traceback (most recent call last):
File “”, line 1, in
File “/tmp/pip-install-6HfDE3/gssapi/setup.py”, line 109, in
raise Exception("Could not find main GSSAPI shared library. Please "
Exception: Could not find main GSSAPI shared library. Please try setting GSSAPI_MAIN_LIB yourself or setting ENABLE_SUPPORT_DETECTION to ‘false’

Command “python setup.py egg_info” failed with error code 1 in /tmp/pip-install-6HfDE3/gssapi/

4.关于error: command ‘gcc’ failed with exit status 1错误的解决方法

安装kafka-python

初始化环境变量

Python操作kafka样例

from kafka import KafkaProducer
from kafka.errors import KafkaError
import os

class Kafka_Producer():
    def __init__(self, kafkahost, kafkaport, kafkatopic):
        self.kafkaHost = kafkahost
        self.kafkaPort = kafkaport
        self.kafkatopic = kafkatopic
        self.producer = KafkaProducer(
                bootstrap_servers = '{kafka_host}:{kafka_port}'.format(kafka_host=self.kafkaHost,kafka_port=self.kafkaPort),
                security_protocol="SASL_PLAINTEXT",
                sasl_mechanism="GSSAPI",
                sasl_kerberos_service_name="kafka",
                compression_type='gzip' #压缩方式
                )
    def sendFileData(self, params):
        try:
             f = open(params,'rb')
             parmasMessage = f.read(-1).strip()
             producer = self.producer
             producer.send(self.kafkatopic, parmasMessage)
             producer.flush()
        except KafkaError as e:
             print (e)

def main():
    filePath = "/home/public/data/"
    topic = "demo"
    producer = Kafka_Producer("xxx.xx.xx.xx","9092",topic)
    dirList = os.listdir(filePath)
    for fileName in dirList:
        producer.sendFileData(filePath+fileName)
    print('send success!!!')

if __name__=='__main__':
    main()

文稿实践参考如下::
[1].https://www.cnblogs.com/qiannianyuan/p/greenplum_compile.html
[2].https://blog.csdn.net/weixin_28738845/article/details/82598386
[3].https://blog.csdn.net/liao392781/article/details/80776528

文稿内容,仅作学习记录

12-15 03:14