-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·105 lines (99 loc) · 4.7 KB
/
main.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
#!/usr/bin/env python2
import user
import termextract
import time
import BaseHTTPServer
import json
from cgi import parse_header, parse_multipart
from urlparse import parse_qs
import sys, os
from config import config
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def parse_POST(self):
ctype, pdict = parse_header(self.headers['content-type'])
if ctype == 'multipart/form-data':
postvars = parse_multipart(self.rfile, pdict)
elif ctype == 'application/x-www-form-urlencoded':
length = int(self.headers['content-length'])
postvars = parse_qs(
self.rfile.read(length),
keep_blank_values=1)
else:
postvars = {}
return postvars
def do_HEAD(s):
s.send_response(200)
s.send_header("Content-type", "application/json")
s.end_headers()
def do_GET(s):
"""Respond to a GET request."""
s.send_response(200)
s.send_header("Content-type", "text/html")
s.end_headers()
s.wfile.write("""<html><head><title>Title goes here.</title></head>
<body><form method="post">
<textarea name="data" cols="120" rows="30">
{
"user": "testUser",
"actionGetRating": true,
"actionModRatings": true,
"modRatings": { "document": 1.0 },
"actionDeleteUser": false,
"text": "Google just settled video codec patent claims with MPEG LA and its VP8 format, which it wants to be elevated to an Internet standard, already faces the next round of patent infringement allegations. Nokia submitted an IPR declaration to the Internet Engineering Task Force listing 64 issued patents and 22 pending patent applications it believes are essential to VP8. To add insult to injury, Nokia's declaration to the IETF says NO to royalty-free licensing and also NO to FRAND (fair, reasonable and non-discriminatory) licensing. Nokia reserves the right to sue over VP8 and to seek sales bans without necessarily negotiating a license deal. Two of the 86 declared IPRs are already being asserted in Mannheim, Germany, where Nokia is suing HTC in numerous patent infringement cases. A first VP8-related trial took place on March 8 and the next one is scheduled for June 14. In related Nokia-Google patent news, the Finns are trying to obtain a U.S. import ban against HTC to force it to disable tethering (or, more likely, to pay up)."
}
</textarea><br />
<input type="submit" value="send"></form>""")
s.wfile.write("<p>You accessed path: %s</p>" % s.path)
s.wfile.write("</body></html>")
def do_POST(s):
"""Respond to a POST request."""
postvars = s.parse_POST()
s.send_response(200)
s.send_header("Content-type", "application/json")
s.send_header("Cache-Control", "private, must-revalidate, max-age=0")
s.send_header("Pragma", "no-cache")
s.end_headers()
if(postvars.has_key('data') and postvars['data'][0]):
data = json.loads(postvars['data'][0])
if(not data):
return
t = user.User(data['user'])
if(data.has_key('actionDeleteUser') and data['actionDeleteUser']):
user.deleteUser(data['user'])
return
if(data.has_key('actionModRatings') and data['actionModRatings']):
for i in data['modRatings'].keys():
t.modifyKeywordRating(i, data['modRatings'][i])
t.saveData()
if(data.has_key('actionGetRating') and data['actionGetRating']):
kws = termextract.getKeywords(data['text'])
data['keywords'] = kws
data['rating'] = t.getRating(kws)
s.wfile.write(json.dumps(data, indent=4))
if __name__ == '__main__':
if(len(sys.argv) > 1):
f = open(sys.argv[1], 'r')
config.conf = json.load(f)
f.close()
hostname = config.conf['hostname']
portNumber = config.conf['port']
datadir = config.conf['datadir']
try:
if not os.path.exists(datadir):
print "Warning: data directory %s doesn't exist, attempting to create it" % (datadir)
os.makedirs(datadir)
except OSError:
print "Error: could not create the non-existant data directory %s" % (datadir)
raise
if(not os.access(datadir, os.W_OK)):
print "Error: data directory %s isn't writable" % (datadir)
sys.exit(1)
serverClass = BaseHTTPServer.HTTPServer
httpd = serverClass((hostname, portNumber), MyHandler)
print time.asctime(), "Server Starts - %s:%s" % (hostname, portNumber)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), "Server Stops - %s:%s" % (hostname, portNumber)