GithubHelp home page GithubHelp logo

ahzhao / python-websocket Goto Github PK

View Code? Open in Web Editor NEW

This project forked from flygsand/python-websocket

0.0 2.0 0.0 97 KB

a simple Python/asyncore WebSocket client library (primarily maintaining compatibility with the Tornado implementation)

License: GNU General Public License v3.0

Python 100.00%

python-websocket's Introduction

python-websocket

python-websocket is a Python/asyncore WebSocket client library.

Why would I want to use WebSocket over plain TCP for my desktop app?

While the primary intention of the WebSocket protocol is to enable "TCP-like" full-duplex communication from within a web apps, it also provides a number of benefits (over plain TCP) for desktop apps:

  • the WebSocket client library is responsible for message parsing, and exposes a simple, event-driven API.
  • WebSocket connections are firewall- and proxy-friendly as they are simply upgraded HTTP connections.
  • HTTP cookies can be attached to the WebSocket handshake, where they can be used for e.g. authentication.

Usage

def my_msg_handler(msg):
  print 'Got "%s"!' % msg

socket = WebSocket('ws://example.com/demo', onmessage=my_msg_handler)
socket.onopen = lambda: socket.send('Hello world!')

try:
  asyncore.loop()
except KeyboardInterrupt:
  socket.close()

API reference

WebSocket(url, protocol=None, cookie_jar=None, onopen=None, onmessage=None, onerror=None, onclose=None)

Returns a WebSocket connected to a remote host at the given url. In order to allow communication over this socket, asyncore.loop() must be called by the client.

The optional protocol parameter can be used to specify the sub-protocol to be used. By providing a cookie_jar (cookielib), appropriate cookies will be sent to the server.

The remaining parameters are callback functions that will be invoked in the following manner:

onopen:    invoked when a connection to the remote host has been
           successfully established

onmessage: invoked when a message is received (passing the
           received data as an argument to the callback 
           function)

onerror:   invoked when a communication error occured (passing 
           an Exception instance as an argument to the callback 
           function). If onerror is not provided, the default 
           asyncore behavior is used (raising the exception)

onclose:   invoked when the connection has closed _normally_ (by
           request from either client or server)

WebSocket.send(data)

Sends data through the socket.

WebSocket.close()

Closes the socket.

python-websocket's People

Contributors

flygsand avatar

Watchers

James Cloos avatar Ahzhao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.