-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
63 lines (53 loc) · 1.59 KB
/
server.py
File metadata and controls
63 lines (53 loc) · 1.59 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
"""
Ilari Shafer
4/30/07
Simple server that allows connection to a few dynamic methods
(provided by OutlookInterface) and serves up static pages
"""
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import urlparse
from _outlook import *
import os
outlook = None
#call the given method with the specified query string
def qs_call(method, qs):
allargs = dict()
alst = qs.split("&")
print alst
for arg in alst:
l = arg.split("=")
if len(l) >= 2:
allargs[l[0]] = l[1]
return method(**allargs)
#define a custom response to HTTP requests
class Handler(BaseHTTPRequestHandler):
#send back the given string to the client, requesting
#no caching if cache is False
def toss(self, str, cache):
self.send_response(200)
self.send_header("Content-type","text-html")
if not cache:
self.send_header("Expires","Mon, 26 Jul 1997 05:00:00 GMT" );
self.send_header("Cache-Control","no-cache, must-revalidate");
self.send_header("Pragma","no-cache");
self.end_headers()
self.wfile.write(str)
#handle GET requests from clients
def do_GET(self):
pstr = urlparse(self.path)
if pstr[2].startswith("/Outlook."):
#invoke a method on our Outlook object
print "Outlook invocation:", pstr
method = getattr(outlook, pstr[2].split(".")[1])
self.toss(qs_call(method, pstr[4]),False)
elif os.path.exists(pstr[2][1:]):
#return files
fh = open(pstr[2][1:],"r")
self.toss(fh.read(),True)
fh.close()
else:
self.send_error(404,"Ugh, error.")
outlook = OutlookInterface()
server = HTTPServer(("",8080),Handler)
print "Serving!"
server.serve_forever()