-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
50 lines (41 loc) · 1.46 KB
/
main.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
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import parse_qs
import SocketServer
import os
import base64
import json
counter = 0
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_POST(self):
global counter
if not '?type=url' in self.path:
filename = self.path[1:]
if not filename:
filename = 'output' + str(counter)
counter = counter + 1
filename = "output/" + filename
filepath = '/'.join(filename.split("/")[:-1])
else:
filepath = "output/urls/"
filename = "output/urls/" + self.path[1:].replace('/','_')
if not os.path.exists(filepath):
os.makedirs(filepath)
data = self.rfile.read(int(self.headers.getheader('content-length')))
with open(filename, 'wb') as f:
try:
x = json.loads(data)
f.write(base64.b64decode(x))
except Exception as err:
f.write(data)
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
def run(server_class=HTTPServer, handler_class=S, port=7878):
server_address = ('0.0.0.0', port)
httpd = server_class(server_address, handler_class)
print 'Starting server'
httpd.serve_forever()
run()