参考: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的更多相关文章

  1. python websocket client 使用

    import websocket ws = websocket.WebSocket() ws.connect("xx.xx.xx") ws.send("string&qu ...

  2. WebSocket client for python

    Project description websocket-client module is WebSocket client for python. This provide the low lev ...

  3. python websocket学习使用

    前言 今天看了一些资料,记录一下心得. websocket是html5引入的一个新特性,传统的web应用是通过http协议来提供支持,如果要实时同步传输数据,需要轮询,效率低下 websocket是类 ...

  4. Python websocket

    一.自己实现websocket 网上流传的都是Python2的websocket实现 # coding=utf8 # !/usr/bin/python import struct, socket im ...

  5. ORA-12518,TNS:listener could not hand off client connection

    前几天在启动应用的时候,在控制台抛出了此异常信息!很明显是数据库方面的问题,不过具体是什么问题哪?百度了一下,网上关于此问题的信息还是有比较多,从异常的提示中我们也能看到是具体是和客户端的连接相关的问 ...

  6. Python WebSocket长连接心跳与短连接

    python websocket 安装 pip install websocket-client 先来看一下,长连接调用方式: ws = websocket.WebSocketApp("ws ...

  7. java websocket client

    websocket是H5新推出的协议,一般用于前端,但是在实际项目中我们需要用java代码来获取一些设备的实时运行数据,在后台处理后推送的前台界面,为了保证实时性,我们需要用到websocket协议, ...

  8. mac虚拟机搭建自动化环境-wda和python wda client

    尽量升级Xcode到最新版,保持iPhone的版本大于9.3 1.安装webDriverAgent到ios真机 从github上下载代码:git clone https://github.com/fa ...

  9. Python 数据库的Connection、Cursor两大对象

    Python 数据库的Connection.Cursor两大对象 pymysql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同. Python 数据库图解流程 Con ...

  10. Python Kafka Client 性能测试

    一.前言 由于工作原因使用到了 Kafka,而现有的代码并不能满足性能需求,所以需要开发高效读写 Kafka 的工具,本文是一个 Python Kafka Client 的性能测试记录,通过本次测试, ...

随机推荐

  1. fuzz for test of the Net::HTTP::GET

    use Net::HTTP::GET; % %0e%0f ' *%26 @.jpg>; my $count = 0; for @chars X @chars X @chars X @chars ...

  2. VS2010上连接SQLite数据库

    VS2010连接SQLite数据库 Visual studio 2010及以上版本,连接SQLite数据库 1.在Sqlite开发站点下载SQLite的.exe安装包 Ctrl+F搜索这条语句:Thi ...

  3. MCS-51 单片机的中断系统

    MCS-51 单片机的中断系统 MCS-51中断系统:5个中断源(两个外部中断, 两个定时器, 一个串口),2个优先级 中断相关概念 中断:当CPU正在处理某件事情时,单片机外部或内部发生的某一紧急事 ...

  4. html可用于跨域的三个标签

    img 应用场景: <img>用于打点统计,统计网站可能是其他域 script 应用场景: 可以用于cdn,还可以用于JSONP link 可用于cdn

  5. CSS------如何让ul中的li分为两列甚至多列

    转载: http://blog.sina.com.cn/s/blog_7f13f92a0100rkfg.html 只需要复制ul和li中的style样式即可 如果需要自定义多少列,只需要修改li中的w ...

  6. SET操作符

    一:MySQL交集INTERSECT运算符 1.介绍 INTERSECT运算符是一个集合运算符,它只返回两个查询或更多查询的交集. 语法: INTERSECT运算符比较两个查询的结果,并返回由左和右查 ...

  7. java学习之租车系统

    ​背景:有三种类型的车供给用户来租用​ ​要求:控制台用户交互界面,根据用户需求输出租车价格,结果如下: 创建租车类主要设计过程: 创建租车类 创建Car父类,包含四种属性成员,重写构造方法 创建三种 ...

  8. 解决 Could not resolve com.android.tools.build:gradle:3.1.3

      android studio 升级到3.1.3后总会遇到莫名其妙的错误,前几天刚解决了项目 dependencies报错的问题. 解决android studio 升级到3.0+之后 项目 dep ...

  9. 把.html转换成.jsp中jqplot画图表不能正常显示,出错的心得

    在做这个的时候,明明html中是完全可行的,如下图: 但后缀名改成.jsp后竟出现如下情况: 这太坑爹了吧,我的图呢! 哎,又要自己找代码问题了,无奈! 先给出我还没修改前的代码吧,关于里面的.js, ...

  10. HashMap分析 + 哈希表

    http://www.cnblogs.com/hzmark/archive/2012/12/24/HashMap.html http://www.cnblogs.com/xqzt/archive/20 ...