2015-11-11 21:29:53 +00:00
|
|
|
import argparse
|
|
|
|
import asyncio
|
2015-11-26 19:37:17 +00:00
|
|
|
import gc
|
2015-11-11 21:29:53 +00:00
|
|
|
import uvloop
|
|
|
|
|
|
|
|
from socket import *
|
|
|
|
|
|
|
|
|
2015-11-26 21:22:39 +00:00
|
|
|
PRINT = 0
|
2015-11-26 19:37:17 +00:00
|
|
|
|
|
|
|
|
2015-11-11 21:29:53 +00:00
|
|
|
async def echo_server(loop, address):
|
|
|
|
sock = socket(AF_INET, SOCK_STREAM)
|
|
|
|
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
|
|
|
sock.bind(address)
|
|
|
|
sock.listen(5)
|
|
|
|
sock.setblocking(False)
|
2015-11-26 19:37:17 +00:00
|
|
|
if PRINT:
|
|
|
|
print('Server listening at', address)
|
2015-11-11 21:29:53 +00:00
|
|
|
with sock:
|
|
|
|
while True:
|
|
|
|
client, addr = await loop.sock_accept(sock)
|
2015-11-26 19:37:17 +00:00
|
|
|
if PRINT:
|
|
|
|
print('Connection from', addr)
|
2015-11-11 21:29:53 +00:00
|
|
|
loop.create_task(echo_client(loop, client))
|
|
|
|
|
|
|
|
|
|
|
|
async def echo_client(loop, client):
|
|
|
|
with client:
|
2016-02-03 23:33:39 +00:00
|
|
|
while True:
|
|
|
|
data = await loop.sock_recv(client, 10000)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
await loop.sock_sendall(client, data)
|
2015-11-26 23:35:00 +00:00
|
|
|
if PRINT:
|
|
|
|
print('Connection closed')
|
2015-11-11 21:29:53 +00:00
|
|
|
|
|
|
|
|
2015-11-11 22:35:03 +00:00
|
|
|
async def echo_client_streams(reader, writer):
|
2015-11-25 17:02:12 +00:00
|
|
|
sock = writer.get_extra_info('socket')
|
2015-11-26 19:37:17 +00:00
|
|
|
if PRINT:
|
|
|
|
print('Connection from', sock.getpeername())
|
2015-11-11 22:35:03 +00:00
|
|
|
while True:
|
|
|
|
data = await reader.read(10000)
|
|
|
|
if not data:
|
|
|
|
break
|
|
|
|
writer.write(data)
|
|
|
|
await writer.drain()
|
2015-11-26 19:37:17 +00:00
|
|
|
if PRINT:
|
|
|
|
print('Connection closed')
|
2016-02-03 23:33:39 +00:00
|
|
|
writer.close()
|
2015-11-26 19:37:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def print_debug(loop):
|
|
|
|
while True:
|
|
|
|
print(chr(27) + "[2J") # clear screen
|
|
|
|
loop.print_debug_info()
|
|
|
|
await asyncio.sleep(0.5, loop=loop)
|
2015-11-11 22:35:03 +00:00
|
|
|
|
|
|
|
|
2015-11-11 21:29:53 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument('--uvloop', default=False, action='store_true')
|
2015-11-11 22:35:03 +00:00
|
|
|
parser.add_argument('--streams', default=False, action='store_true')
|
2016-02-03 23:33:39 +00:00
|
|
|
parser.add_argument('--addr', default='127.0.0.1:25000', type=str)
|
2015-11-26 21:22:39 +00:00
|
|
|
parser.add_argument('--print', default=False, action='store_true')
|
2015-11-11 21:29:53 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
if args.uvloop:
|
|
|
|
loop = uvloop.Loop()
|
|
|
|
print('using UVLoop')
|
|
|
|
else:
|
|
|
|
loop = asyncio.new_event_loop()
|
|
|
|
print('using asyncio loop')
|
|
|
|
|
|
|
|
asyncio.set_event_loop(loop)
|
|
|
|
loop.set_debug(False)
|
|
|
|
|
2015-11-26 21:22:39 +00:00
|
|
|
if args.print:
|
|
|
|
PRINT = 1
|
|
|
|
|
2015-11-26 19:37:17 +00:00
|
|
|
if hasattr(loop, 'print_debug_info'):
|
|
|
|
loop.create_task(print_debug(loop))
|
|
|
|
PRINT = 0
|
|
|
|
|
2016-02-03 23:33:39 +00:00
|
|
|
addr = args.addr.split(':')
|
|
|
|
addr[1] = int(addr[1])
|
|
|
|
addr = tuple(addr)
|
|
|
|
print('serving on: {}'.format(addr))
|
|
|
|
|
2015-11-11 22:35:03 +00:00
|
|
|
if args.streams:
|
|
|
|
print('using asyncio/streams')
|
|
|
|
coro = asyncio.start_server(echo_client_streams,
|
2016-02-03 23:33:39 +00:00
|
|
|
*addr,
|
|
|
|
loop=loop)
|
2015-11-11 22:35:03 +00:00
|
|
|
loop.create_task(coro)
|
|
|
|
else:
|
|
|
|
print('using sock_recv/sock_sendall')
|
2016-02-03 23:33:39 +00:00
|
|
|
loop.create_task(echo_server(loop, addr))
|
2015-11-11 21:29:53 +00:00
|
|
|
try:
|
|
|
|
loop.run_forever()
|
|
|
|
finally:
|
2015-11-26 19:37:17 +00:00
|
|
|
if hasattr(loop, 'print_debug_info'):
|
|
|
|
gc.collect()
|
|
|
|
print(chr(27) + "[2J")
|
|
|
|
loop.print_debug_info()
|
|
|
|
|
2015-11-11 21:29:53 +00:00
|
|
|
loop.close()
|