-
Notifications
You must be signed in to change notification settings - Fork 21
/
server.py
93 lines (72 loc) · 2.62 KB
/
server.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from ds_store import DSStore
import cgi, json, os, sys, posixpath
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
reload(sys)
sys.setdefaultencoding("utf8")
DIR_NAME = os.path.dirname(os.path.realpath(__file__)) + '/userdata'
def parse(file):
filelist = []
for i in file:
if i.filename != '.':
filelist.append(i.filename)
return list(set(filelist))
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.wfile.write("get")
def do_POST(self):
self.send_response(200)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
}
)
for field in form.keys():
field_item = form[field]
if field_item.filename:
filename = os.path.join(DIR_NAME + posixpath.abspath('/' + field_item.filename))
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except:
pass
try:
with open(filename, "wb") as f:
file_data = field_item.file.read()
f.write(file_data)
print('Uploaded:' + field_item.filename)
except: pass
try:
with DSStore.open(filename) as d:
fileresult = parse(d)
self.wfile.write(json.dumps(fileresult, ensure_ascii=False, encoding="utf-8"))
print(filename+': '+ json.dumps(fileresult, ensure_ascii=False, encoding="utf-8"))
except:
pass
def run(server_class=HTTPServer, handler_class=S, port=5000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print 'Server listening on', port
httpd.serve_forever()
try:
if __name__ == "__main__":
from sys import argv
if len(argv) == 2:
run(port=int(argv[1]))
else:
run()
except KeyboardInterrupt:
print '^C received, shutting down the web server'
# httpd.socket.close()