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 ...
随机推荐
- iOS指向函数的指针和block
一:block基础知识 block基础知识 基本概念:block是用来保存一段代码的:^:是block得标志 好比*:是指针的标志 特点:1:保存一段代码: 2:可以有参数和返回值: 3:可以作 ...
- Python Spider
一.网络爬虫 网络爬虫又被称为网络蜘蛛(
- 【03】react 之 创建component
React推出后,出于不同的原因先后出现三种定义react组件的方式,殊途同归:具体的三种方式: 函数式定义的无状态组件 es5原生方式React.createClass定义的组件 es6形式的ext ...
- ios 瀑布流的那些事情
转载: 屎壳郎情调-成长日记 首先要知道:瀑布流的核心就是要获取到图片的长宽 网上的很多例子都是加载本地图片的 对于新手而言 改成加载网络图片的确是有点压力的 因为本地的图片 我们是很容易就能获取到 ...
- 【BZOJ1030】文本生成器(容斥原理,AC自动机,计数DP)
题意:给出n个字符串,求长为m至少包含n个里其中一个的串的字符串一共有多少个,字符集为A到Z,答案对10007取模 n<=60,len<=100 思路:将至少一个转化为所有个数减去没有出现 ...
- Maven一些总结
1.Maven的安装和配置 从Maven的官网上下载Maven的安装包,http://maven.apache.org/download.html. 将其解压在你想安放的目录下. 然后配 ...
- java基础(1-50)-------->超级简单,不信你不会!!!
1:java中的保留字:const&goto; 2:&和&&都可以做逻辑运算符,即运算符两边的表达式都为true,结果才为true,一方为false,则结果为false ...
- SVG描边动画实现过程
准备工具:Adobe AI+PS 1.确定SVG画布的大小,在PS中切出需要描边效果的区域,以此区域的大小做为SVG容器的大小. 2.将PS中切好的图片直接拖拽到AI中 3.使用AI中的钢 ...
- c++语言虚函数实现多态的原理(更新版)
自上一个帖子之间跳过了一篇总结性的帖子,之后再发,今天主要研究了c++语言当中虚函数对多态的实现,感叹于c++设计者的精妙绝伦 c++中虚函数表的作用主要是实现了多态的机制.首先先解释一下多态的概念, ...
- 缺少 Google API 秘钥,因此 Chromium 的部分功能将无法使用
获取密钥(ID)教程: https://www.chromium.org/developers/how-tos/api-keys 获取密钥(ID)地址: https://cloud.google.co ...