-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseminar6.py
More file actions
79 lines (68 loc) · 2.68 KB
/
Copy pathseminar6.py
File metadata and controls
79 lines (68 loc) · 2.68 KB
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
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import urlparse, parse_qs
import json as jason
import sqlite3
from json2html import *
def search(composer, is_json):
connection = sqlite3.connect('scorelib.dat')
cur = connection.cursor()
res = cur.execute("select person.name, score.name, print.id "
"from person left join score_author on person.id=score_author.composer "
"join score on score_author.score=score.id "
"join edition on edition.score = score.id "
"join print on print.edition = edition.id "
"where person.name like ? order by person.name", ("%{}%".format(composer),))
out = []
for row in res:
data = {}
data['Composer'] = row[0]
data['Score'] = row[1]
data['Print Number'] = row[2]
out.append(data)
json_out = jason.dumps(out, indent=2)
connection.close()
if is_json:
return json_out
else:
return json2html.convert(json=json_out)
class server(BaseHTTPRequestHandler):
def do_GET(self):
parsed = urlparse(self.path[1:])
format_ = 'html'
is_json = False
composer = ''
body = ''
if parsed.path == 'result':
query = parse_qs(parsed.query)
if 'f' in query.keys() and 'json' == query['f'][0]:
is_json = True
format_ = 'json'
if 'q' in query.keys():
composer = query['q'][0]
body = search(composer, is_json)
else:
form = "<form action=\"/result\" method=\"get\">" \
"<p>composer:<input type=\"text\" name=\"q\"></p>" \
"<p><input type=\"radio\" name=\"f\" value=\"html\" checked>Html</p>" \
"<p><input type=\"radio\" name=\"f\" value=\"json\" >Json</p>" \
"<p><input type=\"submit\" value=\"Submit\"></p></form>"
self.send_response(200)
self.send_header('Content-type', 'text/' + format_ + '; charset=utf8')
self.end_headers()
if parsed.path != 'result':
self.wfile.write(form.encode())
elif is_json:
self.wfile.write(body.encode())
else:
self.wfile.write(bytes('<html><head><title>title</title></head><body><p>%s</p></body></html>' %body, 'utf8'))
address = ('localhost', 8000)
httpd = HTTPServer(address, server)
def start():
print("server running")
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print("server stopped")
start()