| 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 |
|
| 13 |
import eventlet |
| 14 |
|
| 15 |
|
| 16 |
def handle(fd): |
| 17 |
print("client connected") |
| 18 |
while True: |
| 19 |
# pass through every non-eof line |
| 20 |
x = fd.readline() |
| 21 |
if not x: |
| 22 |
break |
| 23 |
fd.write(x) |
| 24 |
fd.flush() |
| 25 |
print("echoed", x, end=' ') |
| 26 |
print("client disconnected") |
| 27 |
|
| 28 |
print("server socket listening on port 6000") |
| 29 |
server = eventlet.listen(('0.0.0.0', 6000)) |
| 30 |
pool = eventlet.GreenPool() |
| 31 |
while True: |
| 32 |
try: |
| 33 |
new_sock, address = server.accept() |
| 34 |
print("accepted", address) |
| 35 |
pool.spawn_n(handle, new_sock.makefile('rw')) |
| 36 |
except (SystemExit, KeyboardInterrupt): |
| 37 |
break |