forked from n6il/pyDriveWire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwhttpserver.py
131 lines (116 loc) · 4.18 KB
/
dwhttpserver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import parse_qs
import cgi
import threading
from dwcommand import DWParser
import os
import tempfile
import base64
import sys
"""
class DWParser:
def __init__(self, server):
self.server = server
def parse(self, data):
return data
"""
parser = None
class GP(BaseHTTPRequestHandler):
def _set_headers(self, ctype, response):
self.send_response(response)
self.send_header('Content-type', ctype)
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
# def do_HEAD(self):
# self._set_headers()
def do_GET(self):
response = 200
if getattr(sys, 'frozen', False):
# we are running in a bundle
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
if self.path in ['/', '/index.html']:
path = os.path.join(bundle_dir, "ui", 'pyDriveWireUi.html')
else:
path = os.path.join(bundle_dir, "ui", self.path[1:])
if os.path.exists(path):
response = 200
else:
response = 404
self._set_headers('text/html', response)
if response != 200:
self.wfile.write(
"<html><body><h1>%d Error: Invalid location: %s</h1></body></html>" %
(response, self.path))
return
# print parse_qs(self.path[2:])
# self.wfile.write("<html><body><h1>Get Request Received!</h1></body></html>")
with open(path) as f:
self.wfile.write(f.read())
def do_POST(self):
global parser
clen, pdict = cgi.parse_header(
self.headers.getheader('Content-Length'))
mtype, pdict = cgi.parse_header(self.headers.getheader('Content-Type'))
data = self.rfile.read(int(clen))
# print "POST path: %s" % self.path
response = 200
if self.path.startswith('/upload'):
qm = self.path.find('?')
if qm > 0:
qd = parse_qs(self.path[qm + 1:])
name = qd['name'][0]
drive = qd['drive'][0]
print "upload drive: %s name: %s" % (drive, name)
# fileName = tempfile.mktemp(prefix=name.split('/')[-1].split('.')[0], suffix='.'+name.split('.')[-1])
fileName = os.path.join(tempfile.gettempdir(), name)
print fileName
with open(fileName, 'wb') as f:
comma = data.index(',')
f.write(base64.b64decode(data[comma + 1:]))
data = 'dw disk insert %s %s' % (drive, fileName)
response = 200
msg = "OK: drive:%s name:%s" % (drive, name)
else:
response = 404
msg = "%d: Error: Invalid upload specification: %s" % (
response, self.path)
self._set_headers('text/html', response)
self.wfile.write(
"<html><body><h1>%s</h1></body></html>" %
(msg))
return
result = parser.parse(data.lstrip().rstrip()).replace('\r', '')
self._set_headers('text/plain', response)
self.wfile.write(result + '\n')
class DWHttpServer:
def __init__(self, server, port):
global parser
self.port = port
self.server = server
parser = DWParser(self.server)
self.thread = threading.Thread(
target=self.run, args=(), kwargs={
'port': self.port})
self.thread.daemon = True
self.thread.start()
def run(self, server_class=HTTPServer, handler_class=GP, port=8088):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Server running at localhost:%s...' % port
httpd.serve_forever()
"""
def start():
t = threading.Thread(target=run, args=())
t.daemon = True
t.start()
return t
t = start()
wdata = raw_input()
"""
if __name__ == '__main__':
r = DWHttpServer(None, 8088)
wdata = raw_input()
# vim: ts=4 sw=4 sts=4 expandtab