-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
75 lines (65 loc) · 2.45 KB
/
webserver.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
# encoding=utf-8
import io
import shutil
import urllib.request
import pymysql as mysql_module
import json
from http.server import BaseHTTPRequestHandler, HTTPServer
TABLE = 'user_info'
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
path, args = urllib.request.splitquery(self.path) # ?分割
arg_item = args.split('=')
user_info = self.getUserInfo(arg_item[1])
self.output_txt(user_info)
def getUserInfo(self, args):
if len(args) != 11 :
return json.dumps({})
# 连接数据库
conn = mysql_module.connect(user='root', passwd='123456',
host='localhost', port=3306, db='ring')
conn.set_charset('utf8')
cur = conn.cursor()
select = "select name, sex, company, phone_number, interest, timestamp from " + TABLE + \
' where belong_to = "' + args + '" and inquire = 0'
cur.execute(select)
user_info = cur.fetchone()
if user_info == None :
cur.close()
conn.commit()
conn.close()
return json.dumps({})
else :
update = "update " + TABLE + " SET inquire = 1 where belong_to = " + args + " and timestamp=" + str(user_info[5])
cur.execute(update)
cur.close()
conn.commit()
conn.close()
result = {}
result['name'] = user_info[0]
result['sex'] = user_info[1]
result['company'] = user_info[2]
result ['phone'] = user_info[3]
result['interest'] = user_info[4]
return json.dumps(result)
def do_POST(self):
path, args = urllib.request.splitquery(self.path)
length = int(self.headers['content-length'])
data = self.rfile.read(length)
self.output_txt(path + args.decode() + data)
def output_txt(self, content):
# 指定返回编码
enc = "UTF-8"
content = content.encode(enc)
f = io.BytesIO()
f.write(content)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "json; charset=%s" % enc)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
shutil.copyfileobj(f, self.wfile)
PORT = 12138
server = HTTPServer(('', PORT), MyRequestHandler)
print('Started httpServer on port ', PORT)
server.serve_forever()