Add python-eventlet 0.16.1
[packages/trusty/python-eventlet.git] / eventlet / examples / echoserver.py
1 #! /usr/bin/env python
2 """\
3 Simple server that listens on port 6000 and echos back every input to
4 the client.  To try out the server, start it up by running this file.
5
6 Connect to it with:
7   telnet localhost 6000
8
9 You terminate your connection by terminating telnet (typically Ctrl-]
10 and then 'quit')
11 """
12 from __future__ import print_function
13
14 import eventlet
15
16
17 def handle(fd):
18     print("client connected")
19     while True:
20         # pass through every non-eof line
21         x = fd.readline()
22         if not x:
23             break
24         fd.write(x)
25         fd.flush()
26         print("echoed", x, end=' ')
27     print("client disconnected")
28
29 print("server socket listening on port 6000")
30 server = eventlet.listen(('0.0.0.0', 6000))
31 pool = eventlet.GreenPool()
32 while True:
33     try:
34         new_sock, address = server.accept()
35         print("accepted", address)
36         pool.spawn_n(handle, new_sock.makefile('rw'))
37     except (SystemExit, KeyboardInterrupt):
38         break