summaryrefslogtreecommitdiff
path: root/snapmesh_plain.py
blob: f89eebf95e0aa8236c117f3e8993c8809650bba8 (plain)
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
#!/usr/bin/env python2
#Snap! extension base by Technoboy10
#features by Timo

data = dict()

import SimpleHTTPServer
class CORSHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
    def send_head(self):
        path = self.path
        ospath = os.path.abspath('')
        if '/put' in path:
            regex = re.compile("\/put\?value=(.*?)&key=(.*)")
            m = regex.match(path)
            value = urllib2.unquote(m.group(1))
            key = urllib2.unquote(m.group(2))
            data[key] = value
            return

        if '/check' in path:
            regex = re.compile("\/check\?key=(.*)")
            m = regex.match(path)
            key = urllib2.unquote(m.group(1))
            f = open(ospath + '/return', 'w+')
            f.write(str(key in data))
            f.close()
        if '/get' in path:
            regex = re.compile("\/get\?key=(.*)")
            m = regex.match(path)
            key = urllib2.unquote(m.group(1))
            if not key in data:
                return ''

            f = open(ospath + '/return', 'w+')
            f.write(str(data[key]))
            f.close()
	if '/list' in path:
            f = open(ospath + '/return', 'w+')
            f.write(data.keys().join('\n'))
            f.close()

        f = open(ospath + '/return', 'rb')
        ctype = self.guess_type(ospath + '/return')
        self.send_response(200)
        self.send_header("Content-type", ctype)
        fs = os.fstat(f.fileno())
        self.send_header("Content-Length", str(fs[6]))
        self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        return f

if __name__ == "__main__":
    import os
    import re
    import SocketServer
    import urllib2
    PORT = 9876 #Pick a port number

    Handler = CORSHTTPRequestHandler
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    httpd.serve_forever()