Python通过kerberos安全认证操作kafka方式
如何通过Kerberos认证.
1.安装Kerberos客户端
CentOS:
yuminstallkrb5-workstation
使用whichkinit查看是否安装成功;
2.拷贝Kerberos配置文件
conf目录下krb5.conf和kafka.keytab和jaas.conf拷贝到客户端机器的etc目录,同时,krb5.conf中的kdc集群主机名和IP配置到客户端机器hosts配置文件中
3.Kinit客户端通过kerberos认证
获取Principal
klist-ktkafka.keytab
4.安装python-gssapi
pipinstallgssapi
遇到的问题,如下:
a.在linux中执行wget命令提示-bash:wget:commandnotfound解决方法
yum-yinstallwget
b.报错:bash:pip:commandnotfound
wgethttps://bootstrap.pypa.io/get-pip.pypythonget-pip.pypip-V#查看pip版本
python-mpipinstall--upgrade--forcepipeasy_install-Usetuptoolspipinstall--upgradesetuptools
3.pip安装出现Command“pythonsetup.pyegg_info”failedwitherrorcode1的解决方法
Traceback(mostrecentcalllast):File“”,line1,inFile“/tmp/pip-install-6HfDE3/gssapi/setup.py”,line109,inraiseException("CouldnotfindmainGSSAPIsharedlibrary.Please"Exception:CouldnotfindmainGSSAPIsharedlibrary.PleasetrysettingGSSAPI_MAIN_LIByourselforsettingENABLE_SUPPORT_DETECTIONto‘false'
Command“pythonsetup.pyegg_info”failedwitherrorcode1in/tmp/pip-install-6HfDE3/gssapi/
yuminstall-ykrb5-devel.x86_64
4.关于error:command‘gcc'failedwithexitstatus1错误的解决方法
yuminstallgccpython-devel
安装kafka-python
pipinstallkafka-python
初始化环境变量
exportKAFKA_OPTS="-Djava.security.auth.login.config=/etc/conf/jaas.conf-Djava.security.krb5.conf=/etc/krb5.conf"
Python操作kafka样例
fromkafkaimportKafkaProducer
fromkafka.errorsimportKafkaError
importos
classKafka_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'#压缩方式
)
defsendFileData(self,params):
try:
f=open(params,'rb')
parmasMessage=f.read(-1).strip()
producer=self.producer
producer.send(self.kafkatopic,parmasMessage)
producer.flush()
exceptKafkaErrorase:
print(e)
defmain():
filePath="/home/public/data/"
topic="demo"
producer=Kafka_Producer("xxx.xx.xx.xx","9092",topic)
dirList=os.listdir(filePath)
forfileNameindirList:
producer.sendFileData(filePath+fileName)
print('sendsuccess!!!')
if__name__=='__main__':
main()
以上这篇Python通过kerberos安全认证操作kafka方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。