-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread_server.py
81 lines (66 loc) · 1.96 KB
/
thread_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
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
from urllib.parse import urlparse
from threading import Thread
from requests.exceptions import (
Timeout,
HTTPError,
ConnectionError
)
import socket
import requests
import logging
log = logging.getLogger(__name__)
SERVER_ADDRESS = (HOST, PORT) = '', 8888
REQUEST_QUEUE_SIZE = 1
def micro_server(server_address):
address_family = socket.AF_INET
socket_type = socket.SOCK_STREAM
request_queue_size = REQUEST_QUEUE_SIZE
# Create a new socket
listen_socket = socket.socket(
address_family,
socket_type
)
# Allow to reuse the same address
listen_socket.setsockopt(
socket.SOL_SOCKET,
socket.SO_REUSEADDR,
1
)
# Bind
listen_socket.bind(server_address)
# Activate
listen_socket.listen(request_queue_size)
while True:
# New client connection
client_connection, client_address = listen_socket.accept()
# Handle request
log.info("Start connected %s" % client_address[0])
Thread(target=handle_request, args=(client_connection,)).start()
def handle_request(client):
log.info("Start process client request")
while True:
request_link = client.recv(1024)
if not request_link:
break
result = process_request(
request_link.decode('utf-8').strip()
)
resp = str(result).encode('utf-8') + b'\n'
client.send(resp)
log.info("Closed connection")
client.close()
def process_request(link):
try:
if not urlparse(link).scheme:
link = "http://" + link
respond = requests.get(link)
return respond.status_code
except (AttributeError, Timeout, HTTPError, ConnectionError):
return 500
if __name__ == '__main__':
logging.basicConfig(level='DEBUG', format="%(threadName)s: %(message)s")
micro_server(SERVER_ADDRESS)