-
Notifications
You must be signed in to change notification settings - Fork 0
/
asyncio_server.py
71 lines (59 loc) · 1.97 KB
/
asyncio_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
from urllib.parse import urlparse
import asyncio
import aiohttp
import logging
import concurrent.futures
log = logging.getLogger(__name__)
SERVER_ADDRESS = (HOST, PORT) = '', 8888
REQUEST_QUEUE_SIZE = 1
def micro_server(server_address):
loop = asyncio.get_event_loop()
coro = asyncio.start_server(handle_request, server_address[0], server_address[1], loop=loop)
server = loop.run_until_complete(coro)
# Serve requests until Ctrl+C is pressed
log.debug('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
finally:
# Close the server
server.close()
loop.close()
@asyncio.coroutine
def handle_request(client_reader, client_writer):
addr = client_writer.get_extra_info('peername')
logging.info('Accepted connection from {}'.format(addr))
while True:
try:
request_link = yield from asyncio.wait_for(client_reader.read(1024), timeout=10.0)
if not request_link:
break
result = yield from process_request(
request_link.decode('utf-8').strip()
)
resp = str(result).encode('utf-8') + b'\n'
client_writer.write(resp)
except concurrent.futures.TimeoutError:
logging.info('Connection from {} closed by timeout'.format(peername))
break
client_writer.close()
@asyncio.coroutine
def process_request(link):
try:
if not urlparse(link).scheme:
link = "http://" + link
respond = yield from aiohttp.get(link)
try:
return respond.status
finally:
respond.close()
except asyncio.TimeoutError:
return 500
if __name__ == '__main__':
logging.basicConfig(level='DEBUG', format="%(threadName)s: %(message)s")
micro_server(SERVER_ADDRESS)