jetblackmessagebus的python3客户端

jetblack-messagebus-python3的Python项目详细描述


jetblack-messagebus-python3型

概述

这是一个用于jetblack-messagebus的Python3.7+客户机。在

它遵循发布-订阅模式,并包括对“通知”的支持 另一个客户的订阅。这使得它能够按需提供数据。在

有关详细信息,请参阅服务器文档。在

示例

下面的客户端在feed“TEST”上订阅主题“FOO”并打印出来 它接收到的数据。在

importasynciofromjetblack_messagebusimportCallbackClientasyncdefon_data(user,host,feed,topic,data_packets,is_image):print(f'data: user="{user}",host="{host}",feed="{feed}",topic="{topic}",is_image={is_image}')ifnotdata_packets:print("no data")else:forpacketindata_packets:message=packet.data.decode('utf8')ifpacket.dataelseNoneprint(f'packet: entitlements={packet.entitlements},message={message}')asyncdefmain():client=awaitCallbackClient.create('localhost',9001)client.data_handlers.append(on_data)awaitclient.add_subscription('TEST','FOO')awaitclient.start()if__name__=='__main__':asyncio.run(main())

SSL

要创建SSL订阅服务器,请传入SSL上下文。在

^{pr2}$

身份验证

消息总线当前支持以下身份验证方法:

  • 密码文件
  • LDAP
  • 智威汤逊

密码文件和LDAP

密码文件和LDAP方法都需要用户名和密码。这个 由基本验证器提供。在

importsslfromjetblack_messagebusimportCallbackClientfromjetblack_messagebus.authenticationimportBasicAuthenticator...asyncdefmain():authenticator=BasicAuthenticator("john.doe@example.com","pa$$word")ssl_context=ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)client=awaitCallbackClient.create('myhost.example.com',9001,ssl=ssl_context,authenticator=authenticator)awaitclient.add_subscription('TEST','FOO')awaitclient.start()

智威汤逊

JWT身份验证要求客户端传递有效令牌。这个 由令牌验证器提供。在

importsslfromjetblack_messagebusimportCallbackClientfromjetblack_messagebus.authenticationimportTokenAuthenticator...asyncdefmain():authenticator=TokenAuthenticator("eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJqb2huLmRvZUBleGFtcGxlLmNvbSIsIm5hbWUiOiJKb2huIERvZSIsImlhdCI6MTUxNjIzOTAyMiwiZXhwIjoxNTE3MTgzNTAxfQ.wLSGBcNUT8r1DqQvaBrrGY4NHiiVOpoxrgeoPsSsJkY")ssl_context=ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)client=awaitCallbackClient.create('myhost.example.com',9001,ssl=ssl_context,authenticator=authenticator)awaitclient.add_subscription('TEST','FOO')awaitclient.start()

消息处理程序

可以接收三种类型的消息:

  • 数据
  • 订阅通知
  • 授权请求。在

数据

数据处理程序如下所示:

# A data handler.asyncdefon_data(user:str,host:str,feed:str,topic:str,data_packets:Optional[List[DataPacket]],is_image:bool)->None:"""Called when data is received"""pass# Add the handler to the client.client.data_handlers.append(on_data)# Remove the handlerclient.data_handlers.remove(on_data)

数据包有两个字段:entitlementsdata。在

entitlements是一组可选的int,它们表示 接收数据所必需的。在

data是一个可选的bytes,保存编码的数据。这代表什么 由发送方和接收方同意。例如,它可能是一个简单的字符串 JSON文本或协议缓冲区。在

订阅通知

订阅通知处理程序如下所示:

# A notification handler.asyncdefon_notification(client_id:UUID,user:str,host:str,feed:str,topic:str,is_add:bool)->None:"""Called for a notification"""pass# Add the handler to the client.client.notification_handlers.append(on_notification)# Remove the handlerclient.notification_handlers.remove(on_notification)

授权请求

# An authorization handler.asyncdefon_authorization(client_id:UUID,host:str,user:str,feed:str,topic:str)->None:"""Called when authorization is requested"""pass# Add the handler to the client.client._authorization_handlers.append(on_authorization)# Remove the handlerclient._authorization_handlers.remove(on_authorization)

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java筛选hibernate中引用的属性   java如何在Bluej中创建以long(integer)为参数的对象   java如何通过JDBC在access中创建新字段   java如何获取格式化日期?   用模板方法模式设计过滤器接口   java编译错误:缺少返回语句   java从JOOQ中的代码生成中排除特定的模式   java小程序生命周期:init()和start()与destroy()和stop()之间的实际区别是什么?   如何在Java中获取类的所有公共静态方法?   匿名onClick方法内的java活动结果   java如何从数千个具有良好性能的寄存器构建excel工作表?   标记Java中多级中断的适当使用   网络化基于Java的Telnet代理服务器,支持TLS。但为什么是java。网SocketException:连接被拒绝   java将magnolia升级到5.6.1,vaadin资源   springcloudnetflixhystrix中的java重写HystrixCommandSpect bean