python websocket-client connection
参考:https://pypi.python.org/pypi/websocket-client/ https://www.cnblogs.com/saryli/p/6702260.html
import websocket
import thread
import time def on_message(ws, message):
print message def on_error(ws, error):
print error def on_close(ws):
print "### closed ###" def on_open(ws):
def run(*args):
for i in range(3):
time.sleep(1)
ws.send("Hello %d" % i)
time.sleep(1)
ws.close()
print "thread terminating..."
thread.start_new_thread(run, ()) if __name__ == "__main__":
websocket.enableTrace(True)
ws = websocket.WebSocketApp("ws://echo.websocket.org/",
on_message = on_message,
on_error = on_error,
on_close = on_close)
ws.on_open = on_open
ws.run_forever()
Short-lived one-off send-receive
This is if you want to communicate a short message and disconnect immediately when done.
from websocket import create_connection
ws = create_connection("ws://echo.websocket.org/")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Receiving..."
result = ws.recv()
print "Received '%s'" % result
ws.close()
If you want to customize socket options, set sockopt.
sockopt example
from websocket import create_connection
ws = create_connection("ws://echo.websocket.org/",
sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY),))
More advanced: Custom class
You can also write your own class for the connection, if you want to handle the nitty-gritty details yourself.
from websocket import create_connection, WebSocket
class MyWebSocket(WebSocket):
def recv_frame(self):
frame = super().recv_frame()
print('yay! I got this frame: ', frame)
return frame ws = create_connection("ws://echo.websocket.org/",
sockopt=((socket.IPPROTO_TCP, socket.TCP_NODELAY),), class_=MyWebSocket)
FAQ
How to disable ssl cert verification?
Please set sslopt to {“cert_reqs”: ssl.CERT_NONE}.
WebSocketApp sample
ws = websocket.WebSocketApp("wss://echo.websocket.org")
ws.run_forever(sslopt={"cert_reqs": ssl.CERT_NONE})
create_connection sample
ws = websocket.create_connection("wss://echo.websocket.org",
sslopt={"cert_reqs": ssl.CERT_NONE})
WebSocket sample
ws = websocket.WebSocket(sslopt={"cert_reqs": ssl.CERT_NONE})
ws.connect("wss://echo.websocket.org")
How to disable hostname verification.
Please set sslopt to {“check_hostname”: False}. (since v0.18.0)
WebSocketApp sample
ws = websocket.WebSocketApp("wss://echo.websocket.org")
ws.run_forever(sslopt={"check_hostname": False})
create_connection sample
ws = websocket.create_connection("wss://echo.websocket.org",
sslopt={"check_hostname": False})
WebSocket sample
ws = websocket.WebSocket(sslopt={"check_hostname": False})
ws.connect("wss://echo.websocket.org")
How to enable SNI?
SNI support is available for Python 2.7.9+ and 3.2+. It will be enabled automatically whenever possible.
Sub Protocols.
The server needs to support sub protocols, please set the subprotocol like this.
Subprotocol sample
ws = websocket.create_connection("ws://exapmle.com/websocket", subprotocols=["binary", "base64"])
wsdump.py
wsdump.py is simple WebSocket test(debug) tool.
sample for echo.websocket.org:
$ wsdump.py ws://echo.websocket.org/
Press Ctrl+C to quit
> Hello, WebSocket
< Hello, WebSocket
> How are you?
< How are you?
Usage
usage:
wsdump.py [-h] [-v [VERBOSE]] ws_url
WebSocket Simple Dump Tool
- positional arguments:
- ws_url websocket url. ex. ws://echo.websocket.org/
- optional arguments:
-
-h, --help show this help message and exit - WebSocketApp
-
-v VERBOSE, --verbose VERBOSE
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏我一杯咖啡【物质支持】,也可以点击下方的【好文要顶】按钮【精神支持】,因为这两种支持都是使我继续写作、分享的最大动力!
python websocket-client connection的更多相关文章
- python websocket client 使用
import websocket ws = websocket.WebSocket() ws.connect("xx.xx.xx") ws.send("string&qu ...
- WebSocket client for python
Project description websocket-client module is WebSocket client for python. This provide the low lev ...
- python websocket学习使用
前言 今天看了一些资料,记录一下心得. websocket是html5引入的一个新特性,传统的web应用是通过http协议来提供支持,如果要实时同步传输数据,需要轮询,效率低下 websocket是类 ...
- Python websocket
一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...
- ORA-12518,TNS:listener could not hand off client connection
前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...
- Python WebSocket长连接心跳与短连接
python websocket 安装 pip install websocket-client 先来看一下,长连接调用方式: ws = websocket.WebSocketApp("ws ...
- java websocket client
websocket是H5新推出的协议,一般用于前端,但是在实际项目中我们需要用java代码来获取一些设备的实时运行数据,在后台处理后推送的前台界面,为了保证实时性,我们需要用到websocket协议, ...
- mac虚拟机搭建自动化环境-wda和python wda client
尽量升级Xcode到最新版,保持iPhone的版本大于9.3 1.安装webDriverAgent到ios真机 从github上下载代码:git clone https://github.com/fa ...
- Python 数据库的Connection、Cursor两大对象
Python 数据库的Connection.Cursor两大对象 pymysql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同. Python 数据库图解流程 Con ...
- Python Kafka Client 性能测试
一.前言 由于工作原因使用到了 Kafka,而现有的代码并不能满足性能需求,所以需要开发高效读写 Kafka 的工具,本文是一个 Python Kafka Client 的性能测试记录,通过本次测试, ...
随机推荐
- __new__[转载]
转载自https://www.cnblogs.com/MnCu8261/p/6365665.html 实际上,实例化类时调用的第一个方法并不是__init__,而是__new__,其作用正是创建并返回 ...
- Ubuntu14.04搭建Android O编译环境
一.搭建环境 官方参考文档: 1.代号.标签和版本号 2.Factory Images 3.Driver Binaries 4.工具链 软硬件版本: 1.系统平台:I5-8500T+8G+1T,Ub ...
- MacOS 下提示APP 损坏 无法安装 解决方法
sudo spctl --master-disable
- ESXi 6.5 总是会话超时
ESXi 6.5 客户端Web界面会话超时 在VMware ESXi 6.5中,主机客户端Web界面会话每15分钟自动超时一次,然后您必须再次重新登录ESXi主机客户端Web界面. 要避免这种繁琐的情 ...
- 【密码学】RSA算法过程-求解密钥
1.密钥的计算获取过程 密钥的计算过程为:首先选择两个质数p和q,令n=p*q. 令k=ϕ(n)=(p−1)(q−1),原理见2的分析 选择任意整数d,保证其与k互质 取整数e,使得[de]k=[1] ...
- USB-HID鼠标、键盘通讯格式【转】
转自:https://blog.csdn.net/techhome803/article/details/9928873 转自:http://www.amobbs.com/forum.php?mod= ...
- python与C交互中传入与读取内存空间
使用用python调用c代码中,从外部传入一个固定大小的内存空间,这段内存需要是可写的 首先看下c中的函数 typedef struct ModelData { unsigned int model_ ...
- yum安装tomcat
http://www.cnblogs.com/liaolongjun/p/5638740.html http://www.awspack.com/os/linux/yum-install-tomcat ...
- UI 框架
Vue.js 之 iView UI 框架 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引用一下,不需要就删除.故观 ...
- hdu 5407(LCM好题+逆元)
CRB and Candies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...