~ubuntu-branches/ubuntu/trusty/websockify/trusty-updates

« back to all changes in this revision

Viewing changes to tests/echo.py

  • Committer: Package Import Robot
  • Author(s): Thomas Goirand
  • Date: 2013-02-23 01:22:51 UTC
  • Revision ID: package-import@ubuntu.com-20130223012251-3qkk1n1p93kb3j87
Tags: upstream-0.3.0+dfsg1
ImportĀ upstreamĀ versionĀ 0.3.0+dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
'''
 
4
A WebSocket server that echos back whatever it receives from the client.
 
5
Copyright 2010 Joel Martin
 
6
Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
 
7
 
 
8
You can make a cert/key with openssl using:
 
9
openssl req -new -x509 -days 365 -nodes -out self.pem -keyout self.pem
 
10
as taken from http://docs.python.org/dev/library/ssl.html#certificates
 
11
'''
 
12
 
 
13
import os, sys, select, optparse
 
14
sys.path.insert(0,os.path.dirname(__file__) + "/../")
 
15
from websocket import WebSocketServer
 
16
 
 
17
class WebSocketEcho(WebSocketServer):
 
18
    """
 
19
    WebSockets server that echos back whatever is received from the
 
20
    client.  """
 
21
    buffer_size = 8096
 
22
 
 
23
    def new_client(self):
 
24
        """
 
25
        Echo back whatever is received.
 
26
        """
 
27
 
 
28
        cqueue = []
 
29
        c_pend = 0
 
30
        cpartial = ""
 
31
        rlist = [self.client]
 
32
 
 
33
        while True:
 
34
            wlist = []
 
35
 
 
36
            if cqueue or c_pend: wlist.append(self.client)
 
37
            ins, outs, excepts = select.select(rlist, wlist, [], 1)
 
38
            if excepts: raise Exception("Socket exception")
 
39
 
 
40
            if self.client in outs:
 
41
                # Send queued target data to the client
 
42
                c_pend = self.send_frames(cqueue)
 
43
                cqueue = []
 
44
 
 
45
            if self.client in ins:
 
46
                # Receive client data, decode it, and send it back
 
47
                frames, closed = self.recv_frames()
 
48
                cqueue.extend(frames)
 
49
 
 
50
                if closed:
 
51
                    self.send_close()
 
52
                    raise self.EClose(closed)
 
53
 
 
54
if __name__ == '__main__':
 
55
    parser = optparse.OptionParser(usage="%prog [options] listen_port")
 
56
    parser.add_option("--verbose", "-v", action="store_true",
 
57
            help="verbose messages and per frame traffic")
 
58
    parser.add_option("--cert", default="self.pem",
 
59
            help="SSL certificate file")
 
60
    parser.add_option("--key", default=None,
 
61
            help="SSL key file (if separate from cert)")
 
62
    parser.add_option("--ssl-only", action="store_true",
 
63
            help="disallow non-encrypted connections")
 
64
    (opts, args) = parser.parse_args()
 
65
 
 
66
    try:
 
67
        if len(args) != 1: raise
 
68
        opts.listen_port = int(args[0])
 
69
    except:
 
70
        parser.error("Invalid arguments")
 
71
 
 
72
    opts.web = "."
 
73
    server = WebSocketEcho(**opts.__dict__)
 
74
    server.start_server()
 
75