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)
数据包有两个字段:entitlements
和data
。在
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)
- 项目
标签: