public
Description: A base class for HTTP based activities for the OLPC laptop
Homepage: https://www.socialtext.net/lukec/index.cgi?httpactivity
Clone URL: git://github.com/lukec/httpactivity.git
httpactivity / XOHTTPServer.py
100644 49 lines (42 sloc) 1.544 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
from SimpleHTTPServer import SimpleHTTPRequestHandler
from BaseHTTPServer import HTTPServer
import XOHTTPStatus
import cgi
 
class XOHTTPRequestHandler(SimpleHTTPRequestHandler):
    # The Browser POSTs to save a file
    def do_POST(self):
        body = cgi.FieldStorage(
                fp=self.rfile,
                headers=self.headers,
                environ = {'REQUEST_METHOD':'POST'},
                keep_blank_values = 1)
        if self.path == '/write':
            self.write_file(body['save_content'].value)
            self.send_response(200)
        else:
            self.send_response(400)
 
    # Serve all static files out of the web subdirectory
    def translate_path(self, path):
        if path == '/':
            path = '/index.html'
        return SimpleHTTPRequestHandler.translate_path(self, '/web' + path)
 
    def write_file(self, content):
        # read the json file to determine where to save the content
        status = XOHTTPStatus.read_status()
        if status['command'] != 'write':
            return
        fh = open(status['filename'], 'w')
        fh.write(content)
        fh.close()
        print "Saved content to %s"%status['filename']
        XOHTTPStatus.initialize()
 
 
def main():
    try:
        server = HTTPServer(('', 4080), XOHTTPRequestHandler)
        print 'Started server at http://localhost:4080'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()
 
if __name__ == '__main__':
    main()