forked from mickelfeng/ProxyPool-1
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
64 lines (52 loc) · 1.84 KB
/
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
# encoding: utf-8
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
import time
import pymysql
from conf import MYSQL_CONF
import random
HOST_NAME = 'localhost'
PORT_NUMBER = 5000
class MyHandler(BaseHTTPRequestHandler):
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
paths = {
'/get': {'status': 200}
}
if self.path in paths:
self.respond(paths[self.path])
else:
self.respond({'status': 500})
def handle_http(self, status_code, path):
self.send_response(status_code)
self.send_header('Content-type', 'text/html')
self.end_headers()
conn = pymysql.connect(host=MYSQL_CONF['host'],
user=MYSQL_CONF['user'],
passwd=MYSQL_CONF['passwd'],
db=MYSQL_CONF['db'],
port=MYSQL_CONF['port'],
charset='utf8')
cursor = conn.cursor()
cursor.execute('select ip,port from proxypool')
ip_list = []
for row in cursor.fetchall():
ip_list.append("%s:%s" % (row[0], row[1]))
content = random.choice(ip_list)
return bytes(content, 'UTF-8')
def respond(self, opts):
response = self.handle_http(opts['status'], self.path)
self.wfile.write(response)
if __name__ == '__main__':
server_class = HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print(time.asctime(), 'Server Starts - %s:%s' % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), 'Server Stops - %s:%s' % (HOST_NAME, PORT_NUMBER))