websocket-client connection( Long-lived )
参考:https://pypi.python.org/pypi/websocket-client/
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
websocket-client connection( Long-lived )的更多相关文章
- websocket通讯协议(10版本)简介
前言: 工作中用到了websocket 协议10版本的,英文的协议请看这里: http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotoc ...
- SpringBoot项目中,WebSocket的使用(观察者设计模式)
1.什么是WebSocket(选择至菜鸟教程(点击跳转),观察者模式) WebSocket 是 HTML5 开始提供的一种在单个 TCP 连接上进行全双工通讯的协议. WebSocket 使得客户端和 ...
- NodeJs 实现 WebSocket 即时通讯(版本二)
服务端代码 websocket.js 'use strict' const WebSocket = require('ws'); const connections = new Map(); cons ...
- WebSocket协议探究(序章)
一 WebSocket协议基于HTTP和TCP协议 与往常一样,进入WebSocket协议学习之前,先进行WebSocket协议抓包,来一个第一印象. WebSocket能实现客户端和服务器间双向.基 ...
- UE4 Run On owing Client解析(RPC测试)
今天看到文档中游戏性指南->远程调用函数->在蓝图中使用远程调用函数的 Run On Owning Client 在所有权的客户端上运行部分,发现把Add Item和Remove Item ...
- springboot websocket集群(stomp协议)连接时候传递参数
最近在公司项目中接到个需求.就是后台跟前端浏览器要保持长连接,后台主动往前台推数据. 网上查了下,websocket stomp协议处理这个很简单.尤其是跟springboot 集成. 但是由于开始是 ...
- websocket初体验(小程序)
之前上个公司做过一个二维码付款功能,涉及到websocket功能,直接上代码 小程序onShow方法下加载: /** 页面的初始数据 **/ data: { code: "", o ...
- Spark运行模式_Spark自带Cluster Manager的Standalone Client模式(集群)
终于说到了体现分布式计算价值的地方了! 和单机运行的模式不同,这里必须在执行应用程序前,先启动Spark的Master和Worker守护进程.不用启动Hadoop服务,除非你用到了HDFS的内容. 启 ...
- NodeJs 实现 WebSocket 即时通讯(版本一)
服务端代码 var ws = require("nodejs-websocket"); console.log("开始建立连接...") var game1 = ...
- Spark运行模式_基于YARN的Resource Manager的Client模式(集群)
现在越来越多的场景,都是Spark跑在Hadoop集群中,所以为了做到资源能够均衡调度,会使用YARN来做为Spark的Cluster Manager,来为Spark的应用程序分配资源. 在执行Spa ...
随机推荐
- MFC点击控件拖动窗口
void CMouseClickDlg::OnLButtonDown(UINT nFlags, CPoint point) { CDialogEx::OnLButtonDown(nFlags, poi ...
- python转exe2
转载自 xiake200704 最终编辑 xiake200704 一.简介 py2exe是一个将python脚本转换成windows上的可独立执行的可执行程序(*.exe)的工具,这样,你 ...
- 【HDOJ5534】Partial Tree(树,背包DP)
题意:有一棵n个点的形态不定的树,每个度为i的节点会使树的权值增加f[i],求树的最大权值 n<=2015,0<=f[i]<=1e4 思路:对不起队友,我再强一点就能赛中出这题了 显 ...
- 《手把手教你学C语言》学习笔记(3)---变量
编程目的是为了解决问题,编程本质是用计算机的思维操作数据,操作就是算法,数据主要是数据类型,也可以说量,其中分为常量和变量,常量主要是指在量的生命周期内无法改变其值:变量主要是指在量的生命周期内可以随 ...
- cobbler一键部署centos7.4(脚本)
执行脚本之前你需要做四件事 1. 关闭防火墙 2.关闭selinux 3.配置163或者阿里云的 yum源 4.上传centos7.4的镜像如下图 [root@cobbler ~]# cat auto ...
- AC日记——[国家集训队2010]小Z的袜子 cogs 1775
[国家集训队2010]小Z的袜子 思路: 传说中的莫队算法(优雅的暴力): 莫队算法是一个离线的区间询问算法: 如果我们知道[l,r], 那么,我们就能O(1)的时间求出(l-1,r),(l+1,r) ...
- html table 使用总结
html中的table是一个历史相当悠久的标签,它能够很方便的实现数据的表格展示.虽然table是个很基础的标签,但是想用好还是对css相关知识有要求的. 由于table标签中自带的属性操作起来略为麻 ...
- [Machine Learning with Python] Cross Validation and Grid Search: An Example of KNN
Train model: from sklearn.model_selection import GridSearchCV param_grid = [ # try 6 (3×2) combinati ...
- java8 之CompletableFuture -- 如何构建异步应用
什么是Future 接口 很多场景下,我们想去获取线程运行的结果,而通常使用execute方法去提交任务是无法获得结果的,这时候我们常常会改用submit方法去提交,以便获得线程运行的结果. 而sub ...
- Bootstrap多层模态框modal嵌套问题
一.问题 在项目里忽然新加了一个需求,在原本弹出的模态框里,点击模态框里面的按钮再弹出一个模态框,出来另个模态框来展示详细信息.此时就存在两个模态框在这个需求没加之前有一个弹出的模态框也是需要继续点击 ...