echoserver.py 967 B

1234567891011121314151617181920212223242526272829303132333435363738
  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. Connect to it with:
  6. telnet localhost 6000
  7. You terminate your connection by terminating telnet (typically Ctrl-]
  8. and then 'quit')
  9. """
  10. from __future__ import print_function
  11. import eventlet
  12. def handle(fd):
  13. print("client connected")
  14. while True:
  15. # pass through every non-eof line
  16. x = fd.readline()
  17. if not x:
  18. break
  19. fd.write(x)
  20. fd.flush()
  21. print("echoed", x, end=' ')
  22. print("client disconnected")
  23. print("server socket listening on port 6000")
  24. server = eventlet.listen(('0.0.0.0', 6000))
  25. pool = eventlet.GreenPool()
  26. while True:
  27. try:
  28. new_sock, address = server.accept()
  29. print("accepted", address)
  30. pool.spawn_n(handle, new_sock.makefile('rw'))
  31. except (SystemExit, KeyboardInterrupt):
  32. break