-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
47 lines (40 loc) · 1.46 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
# coding=utf-8
from http.server import BaseHTTPRequestHandler
import cgi
import time
class PostHandler(BaseHTTPRequestHandler):
def do_POST(self):
print({'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
})
print(self.headers)
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
}
)
self.send_response(200)
self.end_headers()
self.wfile.write(('Client: %s\n' % str(self.client_address)).encode())
self.wfile.write(('User-agent: %s\n' %
str(self.headers['user-agent'])).encode())
self.wfile.write(('Path: %s\n' % self.path).encode())
self.wfile.write(b'Form data:\n')
print(form.keys())
for field in form.keys():
field_item = form[field]
filename = field_item.filename
filevalue = field_item.value
filesize = len(filevalue) # 文件大小(字节)
print(filename, filesize)
with open('%s-' % time.time() + filename, 'wb') as f:
f.write(filevalue)
return
def StartServer():
from http.server import HTTPServer
sever = HTTPServer(("0.0.0.0", 8080), PostHandler)
sever.serve_forever()
if __name__ == '__main__':
StartServer()