-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflector.py
55 lines (44 loc) · 1.62 KB
/
reflector.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
#Copyright Jon Berg , turtlemeat.com
import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
#import pri
import sys
from subprocess import call
class MyHandler(BaseHTTPRequestHandler):
def do_POST(self):
try:
ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
if ctype == 'multipart/form-data':
query=cgi.parse_multipart(self.rfile, pdict)
self.send_response(200)
self.send_header('Content-type', 'application/pdf')
self.send_header('Content-Disposition', 'attachment')
self.end_headers()
tmp_src = "/tmp/convert-inkscape.svg"
tmp_dst = "/tmp/convert-inkscape.pdf"
try:
with open(tmp_src, "w") as f:
f.write(query.get('content')[0])
except Exception as e:
print "Open tmp file failed:", e
call(['cairosvg', tmp_src, '-d', '600', '-f', 'pdf', '-o', tmp_dst])
with open(tmp_dst, "r") as f:
s = f.read()
self.wfile.write(s)
except:
pass
def main():
if len(sys.argv) > 1:
port = int(sys.argv[1])
else:
port = 7777
try:
server = HTTPServer(('', port), MyHandler)
print 'started httpserver...'
server.serve_forever()
except KeyboardInterrupt:
print '^C received, shutting down server'
server.socket.close()
if __name__ == '__main__':
main()