Python通过kerberos安全认证操作kafka方式

作者:小竹筒 时间:2022-12-23 21:39:49 

如何通过Kerberos认证.

1.安装Kerberos客户端

CentOS:

yum install krb5-workstation

使用which kinit查看是否安装成功;

2.拷贝Kerberos配置文件

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

3.Kinit客户端通过kerberos认证

获取Principal

klist -kt kafka.keytab

4.安装python-gssapi

pip install gssapi

遇到的问题,如下:

a.在linux中执行wget命令提示 -bash: wget: command not found 解决方法

yum -y install wget

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

wget https://bootstrap.pypa.io/get-pip.py python get-pip.py pip -V#查看pip版本

python -m pip install --upgrade --force pip easy_install -U setuptools pip install --upgrade setuptools

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/

yum install -y krb5-devel.x86_64

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

yum install gcc python-devel

安装kafka-python

pip install kafka-python

初始化环境变量

export KAFKA_OPTS="-Djava.security.auth.login.config=/etc/conf/jaas.conf -Djava.security.krb5.conf=/etc/krb5.conf"

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()

来源:https://blog.csdn.net/weixin_42261073/article/details/84074470

标签:Python,kerberos,kafka
0
投稿

猜你喜欢

  • Python 调用 Outlook 发送邮件过程解析

    2023-11-17 19:34:57
  • Python中关于Sequence切片的下标问题详解

    2021-05-30 22:49:47
  • Python灰度变换中灰度切割分析实现

    2021-10-05 12:07:49
  • Go语言基础知识点介绍

    2024-04-26 17:22:50
  • python中迭代器(iterator)用法实例分析

    2023-12-16 05:32:51
  • ASP程序中调用函数Now()显示上午下午的问题

    2009-08-27 13:09:00
  • Python使用OpenPyXL处理Excel表格

    2022-07-29 21:10:21
  • 深度剖析使用python抓取网页正文的源码

    2022-09-29 15:53:39
  • echarts动态获取Django数据的实现示例

    2022-08-26 02:29:06
  • python rolling regression. 使用 Python 实现滚动回归操作

    2021-01-11 09:38:07
  • mysql 存储过程 使用小结

    2010-10-25 20:02:00
  • Python3.6使用tesseract-ocr的正确方法

    2023-10-24 20:46:28
  • AJAX实战实现级联选择

    2009-08-21 12:27:00
  • JavaScript状态模式及适配器模式使用讲解

    2024-04-19 10:14:05
  • PyCharm搭建Spark开发环境的实现步骤

    2022-05-21 04:21:17
  • Python 函数基础知识汇总

    2021-12-22 15:47:09
  • ChatGPT帮我看下这段代码有什么问题

    2022-09-02 15:37:20
  • PHP 字符串 小常识

    2023-11-20 22:08:32
  • python爬虫beautiful soup的使用方式

    2023-05-17 08:13:22
  • python基于moviepy实现音视频剪辑

    2023-08-21 14:56:47
  • asp之家 网络编程 m.aspxhome.com