-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowauth.py
More file actions
61 lines (49 loc) · 1.64 KB
/
Copy pathknowauth.py
File metadata and controls
61 lines (49 loc) · 1.64 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
#! /usr/evn/python
import BaseHTTPServer
import cgi
import json
import time
HOST_NAME = '127.0.0.1' # IP to use
PORT_NUMBER = 8080 # Set this to the necessary port
with open('catalog.json','r') as f:
catalog = f.read()
class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(s):
"""Respond to a GET request."""
print time.asctime(), "GET Request"
if s.headers.has_key('x-auth-user'):
acct = s.headers['x-auth-user']
else:
s.send_response(500)
body = catalog % {'account': acct}
s.send_response(200)
s.send_header("Content-type", "application/json")
s.end_headers()
s.wfile.write(body)
def do_POST(s):
"""Respond to a POST request."""
form = cgi.FieldStorage(
fp=s.rfile,
headers=s.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':s.headers['Content-Type'],
})
data = json.loads(form.value)
print form.value
acct = data['auth']['passwordCredentials']['username']
body = catalog % {'account': acct}
print time.asctime(), "POST Request"
s.send_response(200)
s.send_header("Content-type", "application/json")
s.end_headers()
s.wfile.write(body)
if __name__ == '__main__':
server_class = BaseHTTPServer.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)