一、启动一个tcp server,把送来的数据返回。
$ more echoserver.py
#!/usr/bin/env python
"""Simple server that listens on port 16000 and echos back every input to the client.
Connect to it with:
telnet localhost 16000
Terminate the connection by terminating telnet (typically Ctrl-] and then 'quit').
"""
from __future__ import print_function
from gevent.server import StreamServer
# this handler will be run for each incoming connection in a dedicated greenlet
def echo(socket, address):
print('New connection from %s:%s' % address)
socket.sendall(b'Welcome to the echo server! Type quit to exit.\r\n')
# using a makefile because we want to use readline()
rfileobj = socket.makefile(mode='rb')
while True:
line = rfileobj.readline()
if not line:
print("client disconnected")
break
if line.strip().lower() == b'quit':
print("client quit")
break
socket.sendall(line)
print(line)
rfileobj.close()
if __name__ == '__main__':
# to make the ser