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

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

问题描述

任何人都可以推荐一个用于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服务器,其中许多不支持websockets,例如。

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的答案是好的,但已经变得过时与套接字更新(更具体地说,其协议: 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)

....


b $ b

您可能还想阅读python-websockets: https://github.com/mtah/python- websocket

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

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