File: bug167_client.py

package info (click to toggle)
python-ws4py 0.4.2%2Bdfsg1-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 688 kB
  • sloc: python: 4,500; makefile: 140; javascript: 96
file content (65 lines) | stat: -rw-r--r-- 1,625 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def run_threaded():
    from ws4py.client.threadedclient import WebSocketClient
    class EchoClient(WebSocketClient):
        def opened(self):
            self.send("hello")
            
        def closed(self, code, reason=None):
            print(("Closed down", code, reason))

        def received_message(self, m):
            print(m)
            self.close()
            
    try:
        ws = EchoClient('wss://localhost:9000/ws')
        ws.connect()
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close()

def run_tornado():
    from tornado import ioloop
    from ws4py.client.tornadoclient import TornadoWebSocketClient
    class MyClient(TornadoWebSocketClient):
        def opened(self):
            self.send("hello")
            
        def closed(self, code, reason=None):
            print(("Closed down", code, reason))
            ioloop.IOLoop.instance().stop()

        def received_message(self, m):
            print(m)
            self.close()

    ws = MyClient('wss://localhost:9000/ws')
    ws.connect()

    ioloop.IOLoop.instance().start()

def run_gevent():
    from gevent import monkey; monkey.patch_all()
    import gevent
    from ws4py.client.geventclient import WebSocketClient
    
    ws = WebSocketClient('wss://localhost:9000/ws')
    ws.connect()

    ws.send("hello")
    
    def incoming():
        while True:
            m = ws.receive()
            if m is not None:
                print(m)
            else:
                break
            
        ws.close()

    gevent.joinall([gevent.spawn(incoming)])

#run_gevent()
run_threaded()
run_tornado()