This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Websocket server implementation based on Twisted with SSL support.

Requirements

  • Python 2.7+
  • Twisted

Installation

pip install TwistedWebsocket

Testing

python setup.py test

Built-in broadcast server

A server is already integrated into TwistedWebsocket package.

python -m TwistedWebsocket.server

Advanced options

usage: python -m TwistedWebsocket.server [-h] [-p PORT] [-ssl] [-key KEY] [-cert CERT]

Websocket server implementation based on Twisted with SSL support.

optional arguments:
  -h, --help            show this help message and exit
  -p PORT, --port PORT  Change listening port (default 9999).
  -ssl                  Activate SSL.
  -key KEY              Path to your *.key file.
  -cert CERT            Path to yout *.crt file.

API

Frame manager

  • TwistedWebsocket.frame.Frame(buf): Create Frame instance
  • TwistedWebsocket.frame.Frame.message(): Return decoded message from frame instance
  • TwistedWebsocket.frame.Frame.length(): Return frame length from frame instance
  • TwistedWebsocket.frame.Frame.buildMessage(msg, mask=True): (@staticmethod) Create a websocket compatible frame. If mask == True, it will be a client->server frame.

Server Protocol

  • TwistedWebsocket.server.Protocol.onHandshake(header): Callback when HTTP-like client header is received
  • TwistedWebsocket.server.Protocol.onConnect(): Callback when the client is connected
  • TwistedWebsocket.server.Protocol.onDisconnect(): Callback when the client is disconnected
  • TwistedWebsocket.server.Protocol.onMessage(msg): Callback when the client receive a message
  • TwistedWebsocket.server.Protocol.sendMessage(msg): Send a message to the client
  • TwistedWebsocket.server.Protocol.users: Dictionnary ( self == self.clients[self.id] ) off all the clients connected to the server
  • TwistedWebsocket.server.Protocol.id: Client UUID4 id

Exceptions

  • TwistedWebsocket.exception.FrameError
  • TwistedWebsocket.exception.ProtocolError

Package information

  • TwistedWebsocket.server.__VERSION__

Default Implementation

Broadcast server example:

from twisted.internet.protocol import Factory
from twisted.internet import reactor
from TwistedWebsocket.server import Protocol
import re


class WebSocketHandler(Protocol):

  def onHandshake(self, header):
    g = re.search('Origin\s*:\s*(\S+)', header)
    if not g: return
    print "\n[HANDSHAKE] %s origin : %s" % (self.id, g.group(1))

  def onConnect(self):
    print "\n[CONNECTION] %s connected" % self.id
    for _id, user in self.users.items():
      user.sendMessage("%s connected" % self.id)
      print "\n[FRAME] from %s to %s:\n%s connected" % (self.id, _id, self.id)

  def onDisconnect(self):
    print "\n[DISCONNECTION] %s disconnected" % self.id
    for _id, user in self.users.items():
      user.sendMessage("%s disconnected" % self.id)
      print "\n[FRAME] from %s to %s:\n%s disconnected" % (self.id, _id, self.id)

  def onMessage(self, msg):
    for _id, user in  self.users.items():
      user.sendMessage(msg)
      print "\n[FRAME] from %s to %s:\n%s" % (self.id, _id, msg)


class WebSocketFactory(Factory):

  def __init__(self):
    self.users = {}

  def buildProtocol(self, addr):
    return WebSocketHandler(self.users)


reactor.listenTCP(9999, WebSocketFactory())
reactor.run()

WSS support

from twisted.internet.protocol import Factory
from twisted.internet import reactor, ssl
from TwistedWebsocket.server import Protocol
import re

class WebSocketHandler(Protocol):
  ...

class WebSocketFactory(Factory):
  ...

with open("./keys/ssl.localhost.key") as keyFile:
    with open("./keys/ssl.localhost.cert") as certFile:
      cert = ssl.PrivateCertificate.loadPEM(keyFile.read() + certFile.read())

reactor.listenSSL(PORT, WebSocketFactory(), cert.options())
reactor.run()

Generate self-signed SSL certificates:

$> openssl genrsa -out ./keys/ssl.localhost.key 2048
$> openssl req -new -x509 -key ./keys/ssl.localhost.key -out ./keys/ssl.localhost.cert -days 3650 -subj /CN=ssl.localhost

TODO

  • Improve frame building
  • Add WSS example with self-signed certificates
Release History

Release History

0.0.7

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.6.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.6.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.0.6

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
TwistedWebsocket-0.0.7.tar.gz (5.3 kB) Copy SHA256 Checksum SHA256 Source Mar 7, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting