Python 中的 Socket.IO 客户端库 [英] Socket.IO Client Library in Python

查看:86
本文介绍了Python 中的 Socket.IO 客户端库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能推荐一个用于 Python 的 Socket.IO 客户端库?我已经环顾四周,但我唯一能找到的要么是服务器实现,要么依赖于诸如 Twisted 之类的框架.

Can anyone recommend a Socket.IO client library for Python? I've had a look around, but the only ones I can find are either server implementations, or depend on a framework such as Twisted.

我需要一个不依赖其他框架的客户端库.

I need a client library that has no dependencies on other frameworks.

仅使用多种连接类型中的一种是不够的,因为 python 客户端需要使用多个 socketio 服务器,例如,其中许多不支持 websocket.

Simply using one of the many connection types isn't sufficient, as the python client will need to work with multiple socketio servers, many of which won't support websockets, for example.

推荐答案

Archie1986 的回答很好,但是随着 socketio 的更新已经过时了(更具体地说,它的协议:https://github.com/LearnBoost/socket.io-spec)...据我所知,您需要先手动执行握手您可以请求传输(例如,websockets)连接...请注意,下面的代码不完整且不安全...首先,它会忽略握手响应中返回的受支持传输列表,并始终尝试获取 websocket... 还假设握手总是成功...不过,这是一个很好的起点

Archie1986's answer was good but has become outdated with socketio updates (more specifically, its protocol : https://github.com/LearnBoost/socket.io-spec)... as far as i can tell, you need to perform the handshake manually before you can ask for a transport (e.g., websockets) connection... note that the code below is incomplete and insecure... for one, it ignores the list of supported transports returned in the handshake response and always tries to get a websocket... also it assumes that the handshake always succeeds... nevertheless, it's a good place to start

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

you might also want to read up on python-websockets: https://github.com/mtah/python-websocket

这篇关于Python 中的 Socket.IO 客户端库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆