comparison pythonServer.py @ 0:d2eb0e6ccaaf

initial commit
author Giulio Moro <giuliomoro@yahoo.it>
date Wed, 20 Apr 2016 16:26:57 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d2eb0e6ccaaf
1 #!/usr/bin/python
2
3 import BaseHTTPServer
4 from os import walk
5 from os import path
6 from os import listdir
7 import inspect
8 import os
9 import urllib2
10 import urlparse
11 import pickle
12 import datetime
13
14 # Go to right folder.
15 scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
16 os.chdir(scriptdir) # does this work?
17
18 PSEUDO_PATH = 'example_eval/'
19 pseudo_files = []
20 for filename in listdir(PSEUDO_PATH):
21 if filename.endswith('.xml'):
22 pseudo_files.append(filename)
23
24 curSaveIndex = 0;
25 curFileName = 'test-0.xml'
26 while(path.isfile('saves/'+curFileName)):
27 curSaveIndex += 1;
28 curFileName = 'test-'+str(curSaveIndex)+'.xml'
29
30 pseudo_index = curSaveIndex % len(pseudo_files)
31
32 print 'URL: http://localhost:8000/index.html'
33
34 def send404(s):
35 s.send_response(404)
36 s.send_header("Content-type", "text/html")
37 s.end_headers()
38
39 def processFile(s):
40 s.path = s.path.rsplit('?')
41 s.path = s.path[0]
42 s.path = s.path[1:len(s.path)]
43 st = s.path.rsplit(',')
44 lenSt = len(st)
45 fmt = st[lenSt-1].rsplit('.')
46 size = path.getsize(urllib2.unquote(s.path))
47 fileDump = open(urllib2.unquote(s.path))
48 s.send_response(200)
49
50 if (fmt[1] == 'html'):
51 s.send_header("Content-type", 'text/html')
52 elif (fmt[1] == 'css'):
53 s.send_header("Content-type", 'text/css')
54 elif (fmt[1] == 'js'):
55 s.send_header("Content-type", 'application/javascript')
56 else:
57 s.send_header("Content-type", 'application/octet-stream')
58 s.send_header("Content-Length", size)
59 s.end_headers()
60 s.wfile.write(fileDump.read())
61 fileDump.close()
62
63 def keygen(s):
64 reply = ""
65 options = s.path.rsplit('?')
66 options = options[1].rsplit('=')
67 key = options[1]
68 print key
69 if os.path.isfile("saves/save-"+key+".xml"):
70 reply = "<response><state>NO</state><key>"+key+"</key></response>"
71 else:
72 reply = "<response><state>OK</state><key>"+key+"</key></response>"
73 s.send_response(200)
74 s.send_header("Content-type", "application/xml")
75 s.end_headers()
76 s.wfile.write(reply)
77 file = open("saves/save-"+key+".xml",'w')
78 file.write("<waetresult key="+key+"/>")
79 file.close();
80
81 def saveFile(self):
82 global curFileName
83 global curSaveIndex
84 options = self.path.rsplit('?')
85 options = options[1].rsplit('=')
86 key = options[1]
87 print key
88 varLen = int(self.headers['Content-Length'])
89 postVars = self.rfile.read(varLen)
90 print "Saving file key "+key
91 file = open('saves/save-'+key+'.xml','w')
92 file.write(postVars)
93 file.close()
94 try:
95 wbytes = os.path.getsize('saves/save-'+key+'.xml')
96 except OSError:
97 self.send_response(200)
98 self.send_header("Content-type", "text/xml")
99 self.end_headers()
100 self.wfile.write('<response state="error"><message>Could not open file</message></response>')
101 self.send_response(200)
102 self.send_header("Content-type", "text/xml")
103 self.end_headers()
104 self.wfile.write('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>')
105 curSaveIndex += 1
106 curFileName = 'test-'+str(curSaveIndex)+'.xml'
107
108 class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
109 def do_HEAD(s):
110 s.send_response(200)
111 s.send_header("Content-type", "text/html")
112 s.end_headers()
113 def do_GET(request):
114 global pseudo_index
115 global pseudo_files
116 global PSEUDO_PATH
117 if(request.client_address[0] == "127.0.0.1"):
118 if (request.path == "/favicon.ico"):
119 send404(request)
120 elif (request.path.split('?',1)[0] == "/keygen.php"):
121 keygen(request);
122 else:
123 if (request.path == '/'):
124 request.path = '/index.html'
125 elif (request.path == '/pseudo.xml'):
126 request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
127 print request.path
128 pseudo_index += 1
129 pseudo_index %= len(pseudo_files)
130 processFile(request)
131 else:
132 send404(request)
133
134 def do_POST(request):
135 if(request.client_address[0] == "127.0.0.1"):
136 if (request.path.rsplit('?',1)[0] == "/save" or request.path.rsplit('?',1)[0] == "/save.php"):
137 saveFile(request)
138 else:
139 send404(request)
140
141 def run(server_class=BaseHTTPServer.HTTPServer,
142 handler_class=MyHandler):
143 server_address = ('', 8000)
144 httpd = server_class(server_address, handler_class)
145 httpd.serve_forever()
146
147 run()