To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / pythonServer-3.py

History | View | Annotate | Download (3.86 KB)

1
from http.server import BaseHTTPRequestHandler, HTTPServer
2
from os import walk
3
from os import path
4
from os import listdir
5
import inspect
6
import os
7
import urllib as urllib2
8
import pickle
9
import datetime
10

    
11
# Go to right folder. 
12
scriptdir = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # script directory
13
os.chdir(scriptdir) # does this work?
14

    
15
PSEUDO_PATH = 'example_eval/'
16
pseudo_files = []
17
for filename in listdir(PSEUDO_PATH):
18
    if filename.endswith('.xml'):
19
        pseudo_files.append(filename)
20

    
21
curSaveIndex = 0;
22
curFileName = 'test-0.xml'
23
while(path.isfile('saves/'+curFileName)):
24
        curSaveIndex += 1;
25
        curFileName = 'test-'+str(curSaveIndex)+'.xml'
26

    
27
pseudo_index = curSaveIndex % len(pseudo_files)
28

    
29
print('URL: http://localhost:8000/index.html')
30

    
31
def send404(s):
32
        s.send_response(404)
33
        s.send_header("Content-type", "text/html")
34
        s.end_headers()
35
        
36
def processFile(s):
37
        s.path = s.path.rsplit('?')
38
        s.path = s.path[0]
39
        s.path = s.path[1:len(s.path)]
40
        st = s.path.rsplit(',')
41
        lenSt = len(st)
42
        fmt = st[lenSt-1].rsplit('.')
43
        s.send_response(200)
44
        if (fmt[1] == 'html'):
45
                s.send_header("Content-type", 'text/html')
46
                fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
47
                fileBytes = bytes(fileDump.read(), "utf-8")
48
                fileDump.close()
49
        elif (fmt[1] == 'css'):
50
                s.send_header("Content-type", 'text/css')
51
                fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
52
                fileBytes = bytes(fileDump.read(), "utf-8")
53
                fileDump.close()
54
        elif (fmt[1] == 'js'):
55
                s.send_header("Content-type", 'application/javascript')
56
                fileDump = open(urllib2.parse.unquote(s.path), encoding='utf-8')
57
                fileBytes = bytes(fileDump.read(), "utf-8")
58
                fileDump.close()
59
        else:
60
                s.send_header("Content-type", 'application/octet-stream')
61
                fileDump = open(urllib2.parse.unquote(s.path), 'rb')
62
                fileBytes = fileDump.read()
63
                fileDump.close()
64
        s.send_header("Content-Length", len(fileBytes))
65
        s.end_headers()
66
        s.wfile.write(fileBytes)
67
        
68
def saveFile(self):
69
        global curFileName
70
        global curSaveIndex
71
        varLen = int(self.headers['Content-Length'])
72
        postVars = self.rfile.read(varLen)
73
        print(curFileName)
74
        file = open('saves/'+curFileName,'w')
75
        file.write(postVars.decode("utf-8"))
76
        file.close()
77
        try:
78
                wbytes = os.path.getsize('saves/'+curFileName)
79
        except OSError:
80
                self.send_response(200)
81
                self.send_header("Content-type", "text/xml")
82
                self.end_headers()
83
                self.wfile.write('<response state="error"><message>Could not open file</message></response>')
84
        self.send_response(200)
85
        self.send_header("Content-type", "text/xml")
86
        self.end_headers()
87
        self.wfile.write(bytes('<response state="OK"><message>OK</message><file bytes="'+str(wbytes)+'">"saves/'+curFileName+'"</file></response>','utf-8'))
88
        curSaveIndex += 1
89
        curFileName = 'test-'+str(curSaveIndex)+'.xml'
90

    
91
class MyHandler(BaseHTTPRequestHandler):
92
        def do_HEAD(s):
93
                s.send_response(200)
94
                s.send_header("Content-type", "text/html")
95
                s.end_headers()
96
        def do_GET(request):
97
                global pseudo_index
98
                global pseudo_files
99
                global PSEUDO_PATH
100
                if(request.client_address[0] == "127.0.0.1"):
101
                        if (request.path == "/favicon.ico"):
102
                                send404(request)
103
                        else:
104
                                if (request.path == '/'):
105
                                        request.path = '/index.html'
106
                                elif (request.path == '/pseudo.xml'):
107
                                        request.path = '/'+PSEUDO_PATH + pseudo_files[pseudo_index]
108
                                        print(request.path)
109
                                        pseudo_index += 1
110
                                        pseudo_index %= len(pseudo_files)
111
                                processFile(request)
112
                else:
113
                        send404(request)
114

    
115
        def do_POST(request):
116
                if(request.client_address[0] == "127.0.0.1"):
117
                        if (request.path == "/save" or request.path == "/save.php"):
118
                                saveFile(request)
119
                else:
120
                        send404(request)
121

    
122
def run(server_class=HTTPServer,
123
        handler_class=MyHandler):
124
    server_address = ('', 8000)
125
    httpd = server_class(server_address, handler_class)
126
    httpd.serve_forever()
127

    
128
run()