├── chat ├── requirements.txt ├── reader.py ├── tutorial1.py ├── client_dontwork.py ├── tutorial2.py ├── tutorial3.py ├── client.html ├── tutorial4.py └── server.py └── README.md /chat/requirements.txt: -------------------------------------------------------------------------------- 1 | websockets==2.4 2 | aiohttp==0.15.3 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A simple async chat server written in Python. 2 | 3 | 4 | [](https://gitter.im/oursky/pycon2015?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) -------------------------------------------------------------------------------- /chat/reader.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import asyncio 4 | import websockets 5 | 6 | @asyncio.coroutine 7 | def hello(): 8 | websocket = yield from websockets.connect('ws://localhost:8765/') 9 | while True: 10 | msg = yield from websocket.recv() 11 | if msg is None: 12 | break 13 | print("< {}".format(msg)) 14 | 15 | asyncio.get_event_loop().run_until_complete(hello()) 16 | 17 | -------------------------------------------------------------------------------- /chat/tutorial1.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import asyncio 4 | import cgi 5 | import datetime 6 | import websockets 7 | 8 | 9 | def welcome_msg(): 10 | return "Welcome from server! Time now: {}".format( 11 | datetime.datetime.now().strftime('%H:%M:%S')) 12 | 13 | # ANSWER 14 | #@asyncio.coroutine 15 | #def hello(websocket, path): 16 | # yield from websocket.send(welcome_msg()) 17 | 18 | 19 | 20 | 21 | #FIXME 22 | def hello(websocket, path): 23 | websocket.send(welcome_msg()) #FIXME 24 | 25 | 26 | 27 | 28 | server = websockets.serve(hello, 'localhost', 8765) 29 | asyncio.get_event_loop().run_until_complete(server) 30 | asyncio.get_event_loop().run_forever() 31 | -------------------------------------------------------------------------------- /chat/client_dontwork.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import asyncio 4 | import websockets 5 | 6 | 7 | @asyncio.coroutine 8 | def hello(): 9 | websocket = yield from websockets.connect('ws://localhost:8765/') 10 | asyncio.get_event_loop().create_task(display(websocket)) 11 | 12 | # Note: This does not work. It is because `input` blocks 13 | # the event loop. 14 | #while True: 15 | # name = input("Your message: ") 16 | # yield from websocket.send(name) 17 | # print("> {}".format(name)) 18 | 19 | 20 | @asyncio.coroutine 21 | def display(socket): 22 | while True: 23 | msg = yield from socket.recv() 24 | if msg is None: 25 | asyncio.get_event_loop().stop() 26 | break 27 | 28 | 29 | asyncio.get_event_loop().run_until_complete(hello()) 30 | -------------------------------------------------------------------------------- /chat/tutorial2.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import asyncio 4 | import datetime 5 | import time 6 | import websockets 7 | 8 | 9 | def welcome_msg(): 10 | return "Welcome from server! Time now: {}".format( 11 | datetime.datetime.now().strftime('%H:%M:%S')) 12 | 13 | # ANSWER 14 | #@asyncio.coroutine 15 | #def hello(websocket, path): 16 | # while True: 17 | # yield from websocket.send(welcome_msg()) 18 | # yield from asyncio.sleep(1) 19 | 20 | 21 | 22 | 23 | @asyncio.coroutine 24 | def hello(websocket, path): 25 | while True: 26 | yield from websocket.send(welcome_msg()) 27 | time.sleep(5) #FIXME 28 | 29 | 30 | 31 | 32 | server = websockets.serve(hello, 'localhost', 8765) 33 | asyncio.get_event_loop().run_until_complete(server) 34 | asyncio.get_event_loop().run_forever() 35 | -------------------------------------------------------------------------------- /chat/tutorial3.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import asyncio 4 | import cgi 5 | import datetime 6 | import websockets 7 | 8 | 9 | def welcome_msg(): 10 | return "Welcome from server! Time now: {}".format( 11 | datetime.datetime.now().strftime('%H:%M:%S')) 12 | 13 | 14 | 15 | sockets = [] 16 | 17 | 18 | 19 | 20 | @asyncio.coroutine 21 | def hello(websocket, path): 22 | yield from websocket.send(welcome_msg()) 23 | sockets.append(websocket) 24 | while True: 25 | msg = yield from websocket.recv() 26 | if msg is None: 27 | break 28 | msg = cgi.escape(msg) 29 | for s in sockets: 30 | yield from s.send("New message: " + msg) 31 | 32 | sockets.remove(websocket) 33 | 34 | 35 | 36 | 37 | server = websockets.serve(hello, 'localhost', 8765) 38 | asyncio.get_event_loop().run_until_complete(server) 39 | asyncio.get_event_loop().run_forever() 40 | -------------------------------------------------------------------------------- /chat/client.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | 11 |