attzonko / websocket-client

websocket client for python

Home Page:https://github.com/websocket-client/websocket-client

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

websocket-client

Our repository has moved to https://github.com/websocket-client/websocket-client

websocket-client module is WebSocket client for python. This provide the low level APIs for WebSocket. All APIs are the synchronous functions.

websocket-client supports only hybi-13.

License

  • LGPL

Installation

This module is tested on Python 2.7 and Python 3.x.

Type "python setup.py install" or "pip install websocket-client" to install.

Caution

from v0.16.0, we can install by "pip install websocket-client" for python 3.

This module depend on

  • six
  • backports.ssl_match_hostname for Python 2.x

How about Python 3

Now, we support python 3 on single source code from version 0.14.0. Thanks, @battlemidget and @ralphbean.

HTTP Proxy

Support websocket access via http proxy. The proxy server must allow "CONNECT" method to websocket port. Default squid setting is "ALLOWED TO CONNECT ONLY HTTPS PORT".

Current implementation of websocket-client is using "CONNECT" method via proxy.

example

Examples

Long-lived connection

This example is similar to how WebSocket code looks in browsers using JavaScript.

Short-lived one-off send-receive

This is if you want to communicate a short message and disconnect immediately when done.

If you want to customize socket options, set sockopt.

sockopt example

More advanced: Custom class

You can also write your own class for the connection, if you want to handle the nitty-gritty details yourself.

FAQ

How to disable ssl cert verification?

Please set sslopt to {"cert_reqs": ssl.CERT_NONE}.

WebSocketApp sample

create_connection sample

WebSocket sample

How to disable hostname verification.

Please set sslopt to {"check_hostname": False}. (since v0.18.0)

WebSocketApp sample

create_connection sample

WebSocket sample

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

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 set verbose mode. If set to 1, show opcode. If set to 2, enable to trace websocket module

example:

$ wsdump.py ws://echo.websocket.org/
$ wsdump.py ws://echo.websocket.org/ -v
$ wsdump.py ws://echo.websocket.org/ -vv

About

websocket client for python

https://github.com/websocket-client/websocket-client

License:GNU Lesser General Public License v3.0


Languages

Language:Python 100.0%