changeset 121:b1aca403bb59

Python2.py: Python server will now save received XML files and store them to the local folder 'saves'.
author Nicholas Jillings <nicholas.jillings@eecs.qmul.ac.uk>
date Tue, 26 May 2015 18:43:30 +0100
parents d6251c446b7f
children 0c66c0bf2f69
files python2.py saves/read.txt
diffstat 2 files changed, 34 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/python2.py	Tue May 26 15:00:40 2015 +0100
+++ b/python2.py	Tue May 26 18:43:30 2015 +0100
@@ -2,6 +2,16 @@
 from os import walk
 from os import path
 import urllib2
+import pickle
+import datetime
+
+curSaveIndex = 0;
+curFileName = 'test-0.xml'
+while(path.isfile('saves/'+curFileName)):
+	curSaveIndex += 1;
+	curFileName = 'test-'+str(curSaveIndex)+'.xml'
+
+print curFileName
 
 def send404(s):
 	s.send_response(404)
@@ -30,6 +40,22 @@
 	s.wfile.write(fileDump.read())
 	fileDump.close()
 	
+def saveFile(self):
+	global curFileName
+	global curSaveIndex
+	varLen = int(self.headers['Content-Length'])
+	postVars = self.rfile.read(varLen)
+	print curFileName
+	file = open('saves/'+curFileName,'w')
+	curSaveIndex += 1;
+	curFileName = 'test-'+str(curSaveIndex)+'.xml'
+	print curFileName
+	file.write(postVars)
+	file.close()
+	self.send_response(200)
+	self.send_header("Content-type", "text/xml")
+	self.end_headers()
+	self.wfile.write('<response><state>OK</state><file>saves/'+curFileName+'</file></response>')
 
 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
 	def do_HEAD(s):
@@ -46,6 +72,12 @@
 				processFile(request)
 		else:
 			send404(request)
+	def do_POST(request):
+		if(request.client_address[0] == "127.0.0.1"):
+			if (request.path == "/save"):
+				saveFile(request)
+		else:
+			send404(request)
 
 def run(server_class=BaseHTTPServer.HTTPServer,
         handler_class=MyHandler):
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/saves/read.txt	Tue May 26 18:43:30 2015 +0100
@@ -0,0 +1,2 @@
+This folder is where the python2.py server will save the received test results
+Files will return as test-#.xml where # is the number they were received!