socket.io客户端库
socketIO-client-MV的Python项目详细描述
Socketio客户端
这是一个用于python的socket.io客户端库。您可以使用它为socket.io服务器编写测试代码。
这是Socketio_客户端的分叉版本。你可以找到原始的here。
安装
在隔离环境中安装软件包。
VIRTUAL_ENV=$HOME/.virtualenv # Prepare isolated environment virtualenv $VIRTUAL_ENV # Activate isolated environment source $VIRTUAL_ENV/bin/activate # Install package pip install -U socketIO-client
使用量
激活隔离环境。
VIRTUAL_ENV=$HOME/.virtualenv source $VIRTUAL_ENV/bin/activate
启动socket.io服务器。
cd $(python -c "import os, socketIO_client;\ print(os.path.dirname(socketIO_client.__file__))") DEBUG=* node tests/serve.js # Start socket.io server in terminal one DEBUG=* node tests/proxy.js # Start proxy server in terminal two nosetests # Run tests in terminal three
有关调试信息,请先运行这些命令。
import logging logging.getLogger('socketIO-client').setLevel(logging.DEBUG) logging.basicConfig()
发射。
from socketIO_client import SocketIO, LoggingNamespace with SocketIO('127.0.0.1', 8000, LoggingNamespace) as socketIO: socketIO.emit('aaa') socketIO.wait(seconds=1)
使用回调发出。
from socketIO_client import SocketIO, LoggingNamespace def on_bbb_response(*args): print('on_bbb_response', args) with SocketIO('127.0.0.1', 8000, LoggingNamespace) as socketIO: socketIO.emit('bbb', {'xxx': 'yyy'}, on_bbb_response) socketIO.wait_for_callbacks(seconds=1)
定义事件。
from socketIO_client import SocketIO, LoggingNamespace def on_connect(): print('connect') def on_disconnect(): print('disconnect') def on_reconnect(): print('reconnect') def on_aaa_response(*args): print('on_aaa_response', args) socketIO = SocketIO('127.0.0.1', 8000, LoggingNamespace) socketIO.on('connect', on_connect) socketIO.on('disconnect', on_disconnect) socketIO.on('reconnect', on_reconnect) # Listen socketIO.on('aaa_response', on_aaa_response) socketIO.emit('aaa') socketIO.emit('aaa') socketIO.wait(seconds=1) # Stop listening socketIO.off('aaa_response') socketIO.emit('aaa') socketIO.wait(seconds=1) # Listen only once socketIO.once('aaa_response', on_aaa_response) socketIO.emit('aaa') # Activate aaa_response socketIO.emit('aaa') # Ignore socketIO.wait(seconds=1)
在命名空间中定义事件。
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) self.emit('bbb') socketIO = SocketIO('127.0.0.1', 8000, Namespace) socketIO.emit('aaa') socketIO.wait(seconds=1)
定义标准事件。
from socketIO_client import SocketIO, BaseNamespace class Namespace(BaseNamespace): def on_connect(self): print('[Connected]') def on_reconnect(self): print('[Reconnected]') def on_disconnect(self): print('[Disconnected]') socketIO = SocketIO('127.0.0.1', 8000, Namespace) socketIO.wait(seconds=1)
在单个套接字上定义不同的命名空间。
from socketIO_client import SocketIO, BaseNamespace class ChatNamespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) class NewsNamespace(BaseNamespace): def on_aaa_response(self, *args): print('on_aaa_response', args) socketIO = SocketIO('127.0.0.1', 8000) chat_namespace = socketIO.define(ChatNamespace, '/chat') news_namespace = socketIO.define(NewsNamespace, '/news') chat_namespace.emit('aaa') news_namespace.emit('aaa') socketIO.wait(seconds=1)
通过ssl连接(https://github.com/invisibleroads/socketIO-client/issues/54)。
from socketIO_client import SocketIO # Skip server certificate verification SocketIO('https://127.0.0.1', verify=False) # Verify the server certificate SocketIO('https://127.0.0.1', verify='server.crt') # Verify the server certificate and encrypt using client certificate socketIO = SocketIO('https://127.0.0.1', verify='server.crt', cert=( 'client.crt', 'client.key'))
指定了由于{{A5}库而导致的参数、标头、cookies、代理。
from socketIO_client import SocketIO from base64 import b64encode SocketIO( '127.0.0.1', 8000, params={'q': 'qqq'}, headers={'Authorization': 'Basic ' + b64encode('username:password')}, cookies={'a': 'aaa'}, proxies={'https': 'https://proxy.example.com:8080'})
永远等待。
from socketIO_client import SocketIO socketIO = SocketIO('127.0.0.1', 8000) socketIO.wait()
不要永远等待。
from requests.exceptions import ConnectionError from socketIO_client import SocketIO try: socket = SocketIO('127.0.0.1', 8000, wait_for_connection=False) socket.wait() except ConnectionError: print('The server is down. Try again later.')
许可证
这个软件是在麻省理工学院的许可下提供的。
学分
- Guillermo Rauch写了socket.io specification。
- Hiroki Ohtani写了websocket-client。
- Roderick Hodgson写了一个prototype for a Python client to a socket.io server。
- Alexandre Bourget编写了gevent-socketio,这是一个用python编写的socket.io服务器。
- Paul Kienzle,Zac Lee,Josh VanderLinden,Ian Fitzpatrick,Lucas Klein,Rui Chicoria,Travis Odom,Patrick Huber,Brad Campbell,Daniel,Sean Arietta,Sacha Stafyniak提交了扩展socket.io协议支持的代码。
- Bernard Pratz,Francis Bull编写了支持xhr轮询和jsonp轮询的原型。
- Joe Palmer赞助开发。
- Eric Chen,{A30},Thiago Hersan,Nayef Copty,Jörgen Karlsson,Branden Ghena,Branden Ghena,Tim Landscheidt,Matt Porritt,Matt Dainty,Thomaz de Oliveira dos Reis,Felix König,George Wilson,Andreas Strikos,Alessio Sergi,Claudio Yacarini,Khairi Hafsham,Robbie Clarken,Robbie Clarken,Robbie Clarken,Robbie Clarken,Robbie Clarken,Robbie Clarken,^{a45{A3A33},^{·建议的方法使连接更加牢固。
- Merlijn van Deen,Frederic Sureau,Marcus Cobden,Drew Hutchison,wuurrd,Adam Kecer,Alex Monk,Vishal P R,John Vandenberg,Thomas Grainger,Daniel Quinn,Adric Worley,Adam Roses Wight,Jan Včelák提出的改变,使图书馆对您更加友好和实用!
0.7
- 固定线程清理
- 修复了由于Andreas Strikos直接定义的断开连接检测
- 固定支持Unicode有效载荷
0.6
- 由于Sean Arietta和Joe Palmer,已升级到Socket.io Protocol 1.x
- 修复了对Python3的支持
- 固定SSL支持
- 添加锁以解决轮询传输的并发问题
- 添加了socketio.off()和socketio.once()
0.5
- 增加了对Python3的支持
- 由于bernard pratz,增加了对jsonp轮询的支持
- 由于francis bull,增加了对xhr轮询的支持
- 添加了对查询参数和cookies的支持
- 修复了由于travis odom而在自定义命名空间中发送确认的问题
- 重写库以使用协程而不是线程来节省内存
0.4
- 由于芮和Sajal 增加了对自定义标题和代理的支持
- 由于zac lee,增加了对服务器端回调的支持
- 由于Alexandre Bourget,已将频道功能合并到BaseNamespace中
0.3
- 增加了对安全连接的支持
- 添加了socketio.wait()
- 改进了rhythmicthread和listenerthread中的异常处理
0.2
- 由于Paul Kienzle,增加了对回调和频道的支持
- 采纳了Josh Vanderlinden和Ian Fitzpatrick的建议
0.1
- 包装code from StackOverflow
- 在连接失败时向析构函数添加异常处理