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 的性能测试记录,通过本次测试, ...
随机推荐
- 通过图片获取gps地理位置
别人说通过一张照片就可以定位你的位置,看来个视频,仔细研究了一下自己的照片没想到真的可以做到,想想真的有点可怕. 如何通过一张照片去定位这张照片的经纬度下面我以我手机中的照片为例. 我们通过pytho ...
- BAT修改文本内容
@echo off (for /f "delims=" %%a in (文件名) do ( set "str=%%a" setlocal enabledelay ...
- Socket心跳包机制总结【转】
转自:https://blog.csdn.net/qq_23167527/article/details/54290726 跳包之所以叫心跳包是因为:它像心跳一样每隔固定时间发一次,以此来告诉服务器, ...
- http://blog.csdn.net/u011001723/article/details/45621027
http://blog.csdn.net/u011001723/article/details/45621027 scp + 脚本 config 外置 http://www.cnblogs.com/ ...
- 2011TG初赛
一.单项选择题(共20题,每题1.5分,共计30分,每题有且仅有一个正确选项.) 1. 在二进制下,1011001+( )=1100110. A.1011 B.1101 C.1010 D.1111 B ...
- JAVA学习(二) String使用equals方法和==分别比较的是什么?(转)
String使用的equals方法和==的区别 equals方法和==的区别 首先大家知道,String既可以作为一个对象来使用,又可以作为一个基本类型来使用.这里指的作为一个基本类型来使用只是指 ...
- .NetCore下B/S结构 初探基于遗传学算法的中学自动排课走班(二)
分析下染色体基因 这里用 老师 课程 班级 教室 周天 上下晚 课时作为染色体编码我封装了如下类 /// <summary> /// NP 授课事件 由教室.课程.班级 时间片段构成 li ...
- vue1.0到2.0
vue1.0到2.0 vue2.0 新手教程(一) 想想自己写vue的项目也写了一年了,从vue1.0到2.0,走过不少路,填过不少坑, 下面记录一下新手从0到1的过程,本文“应该”会持续更新 首 ...
- bzoj 1103
题目大意:有一棵树根为1,刚开始每条边的权值为1, 现在有m + n - 1 个操作, A :x y , 将x和y相连的边权值变为1, W:x, 询问x到1路径上的权值和. 思路 : 方法一: ...
- 发送统计邮件shell脚本
#!/bin/bash#作者:Presley#时间:2018.07.16#监控阿里云aaa库的py_weixin_product_close_rating_info 和 py_weixin_produ ...