谁能推荐一个 Python 的 Socket.IO 客户端库?
我环顾四周,但我能找到的唯一的要么是服务器实现,要么依赖于 Twisted 等框架。
我需要一个不依赖于其他框架的客户端库。
仅使用多种连接类型之一是不够的,因为 python 客户端需要使用多个 socketio 服务器,例如,其中许多服务器不支持 websockets。
Archie1986的答案很好,但随着socketio更新已经过时了(更具体地说,它的协议:https://github.com/LearnBoost/socket.io-spec https://github.com/LearnBoost/socket.io-spec)...据我所知,您需要先手动执行握手,然后才能请求传输(例如,websockets)连接...请注意,下面的代码不完整且不安全...一方面,它忽略握手响应中返回的受支持传输列表,并始终尝试获取 Websocket...并且它还假设握手始终成功...尽管如此,这是一个很好的起点
import websocket, httplib
...
'''
connect to the socketio server
1. perform the HTTP handshake
2. open a websocket connection '''
def connect(self) :
conn = httplib.HTTPConnection('localhost:8124')
conn.request('POST','/socket.io/1/')
resp = conn.getresponse()
hskey = resp.read().split(':')[0]
self._ws = websocket.WebSocket(
'ws://localhost:8124/socket.io/1/websocket/'+hskey,
onopen = self._onopen,
onmessage = self._onmessage)
....
您可能还想阅读 python-websockets:https://github.com/mtah/python-websocket https://github.com/mtah/python-websocket
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)