From 40f499401313ebcd9f1d679ed61954b306f738f3 Mon Sep 17 00:00:00 2001 From: schneefux Date: Sat, 21 Feb 2015 16:27:24 +0100 Subject: first commit --- snapmesh_plain.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 snapmesh_plain.py (limited to 'snapmesh_plain.py') diff --git a/snapmesh_plain.py b/snapmesh_plain.py new file mode 100755 index 0000000..f89eebf --- /dev/null +++ b/snapmesh_plain.py @@ -0,0 +1,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() -- cgit v1.3.1