/
server.py
139 lines (113 loc) · 3.98 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/python
# coding=utf-8
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import os
from os import curdir, sep
import json
import re
import db_init
import db
import codecs
import cgi
import urlparse
if not os.path.isfile('db.sqlite3'):
db_init.init()
PORT = 3000
class Handler (BaseHTTPRequestHandler) :
def do_GET(self) :
# Look for main page
if self.path=="/":
self.path="/index.html"
if self.path == "/products" :
#send response code:
self.send_response(200)
#send headers:
self.send_header("Content-type:", "application/json")
# send a blank line to end headers:
self.wfile.write("\n")
#send response:
json.dump(db.getAll(), self.wfile)
if re.match(r'\/product\/\d+', self.path): # match "/product/{ID}"
#send response code:
self.send_response(200)
#send headers:
self.send_header("Content-type:", "application/json")
# send a blank line to end headers:
self.wfile.write("\n")
#send response:
json.dump(db.getOne(re.search(r'[^/]*$', self.path).group(0)), self.wfile) # regex to get everything after the last slash
try:
#Check the file extension required and
#set the right mime type
sendReply = False
if self.path.endswith(".html"):
mimetype='text/html'
sendReply = True
if self.path.endswith(".woff"):
mimetype='font/opentype'
sendReply = True
if self.path.endswith(".ttf"):
mimetype='font/opentype'
sendReply = True
if self.path.endswith(".js"):
mimetype='application/javascript'
sendReply = True
if self.path.endswith(".css"):
mimetype='text/css'
sendReply = True
if sendReply == True:
#Open the static file requested and send it
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type',mimetype)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File Not Found: %s' % self.path)
def do_POST(self):
# Look for POST request
if self.path == "/product":
length = int(self.headers.getheader('content-length'))
# Get form data
postvars = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
# Init data
name = postvars.get('name')[0]
desc = postvars.get('description')[0]
price = float(postvars.get('price')[0])
# Send request to DB
db.putOne(name, desc, price)
# Send code 200
self.send_response(200)
self.end_headers()
def do_PUT(self):
# Look for PUT request
if re.match(r'\/product\/\d+', self.path):
length = int(self.headers.getheader('content-length'))
# Get form data
postvars = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1)
# Init data
name = postvars.get('name')[0]
desc = postvars.get('description')[0]
price = float(postvars.get('price')[0])
# Send request to DB
db.updateOne(re.search(r'[^/]*$', self.path).group(0), name, desc, price)
# Send code 200
self.send_response(200)
self.end_headers()
def do_DELETE(self):
if re.match(r'\/product\/\d+', self.path): # match "/product/{ID}"
#send response code:
self.send_response(200)
#send headers:
self.send_header("Content-type:", "application/json")
# send a blank line to end headers:
self.wfile.write("\n")
#send response:
db.delete(re.search(r'[^/]*$', self.path).group(0)), self.wfile
return
server = HTTPServer(("localhost", PORT), Handler)
print "serving at port", PORT
server.serve_forever()