aboutsummaryrefslogtreecommitdiffstats
path: root/userdata/conf/lava/lava-boot/proxy.py
diff options
context:
space:
mode:
Diffstat (limited to 'userdata/conf/lava/lava-boot/proxy.py')
-rwxr-xr-xuserdata/conf/lava/lava-boot/proxy.py82
1 files changed, 82 insertions, 0 deletions
diff --git a/userdata/conf/lava/lava-boot/proxy.py b/userdata/conf/lava/lava-boot/proxy.py
new file mode 100755
index 0000000..c72efb8
--- /dev/null
+++ b/userdata/conf/lava/lava-boot/proxy.py
@@ -0,0 +1,82 @@
+#!/usr/bin/python
+
+# simple http file upload/download server
+# you can upload either using POST method, or by key/value upload
+
+# GET result/kernelversion/`uname -r`
+# POST method:
+# import requests
+# r = requests.post('http://localhost:8000/', files={'filename': open('proxy.py', 'rb')})
+
+
+import string,cgi,time,os,sys,urllib
+from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
+
+IN_MEMORY_FILES = {}
+
+class MyHandler(BaseHTTPRequestHandler):
+
+ def do_GET(self):
+ try:
+ filename=os.path.basename(self.path)
+ if filename in IN_MEMORY_FILES:
+ self.send_response(200)
+ self.send_header('Content-type','application/octet-stream')
+ self.end_headers()
+ self.wfile.write(IN_MEMORY_FILES[filename])
+ return
+ else:
+ # GET /result?key=value as way to store values with GET
+ # This makes it possible to ping back from lava
+ elements=self.path.split("?")
+ if (len(elements) == 2 and elements[0] == '/result'):
+ (key, data) = elements[1].split("=",1)
+ IN_MEMORY_FILES[key] = urllib.unquote(data)
+ self.send_response(200)
+ self.end_headers()
+ else:
+ self.send_response(404)
+ self.end_headers()
+ return
+ except IOError as e :
+ print e
+ self.send_error(404,'File Not Found: %s' % self.path)
+
+ def do_POST(self):
+ try:
+ ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
+ if ctype == 'multipart/form-data' :
+ fs = cgi.FieldStorage( fp = self.rfile,
+ headers = self.headers, # headers_,
+ environ={ 'REQUEST_METHOD':'POST' }
+ )
+
+ else: raise Exception("Unexpected POST request")
+
+ fs_up = fs['filename']
+ filename = os.path.basename(fs_up.filename)
+ IN_MEMORY_FILES[filename] = fs_up.file.read()
+
+ self.send_response(200)
+ self.end_headers()
+ except Exception as e:
+ print e
+ self.send_error(404,'POST to "%s" failed: %s' % (self.path, str(e)) )
+
+def main():
+ if len(sys.argv) == 2:
+ port = int(sys.argv[1])
+ else:
+ port = 8080
+
+ try:
+ server = HTTPServer(('', port), MyHandler)
+ print 'started file sharing http server at port %d' % port
+ server.serve_forever()
+ except KeyboardInterrupt:
+ print '^C received, shutting down server'
+ server.socket.close()
+
+if __name__ == '__main__':
+ main()
+