-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
119 lines (88 loc) · 3.3 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
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
#!/usr/bin/env python3
import tornado.web
import tornado.ioloop
import os
from time import sleep
import subprocess
from tornado.web import url, authenticated
import random
import string
try:
from secrets import token_urlsafe
except ImportError:
from os import urandom
def token_urlsafe(nbytes=None):
return urandom(nbytes).hex()
HTTP_PORT = 8000
DEBUG = True
supported_extensions = ['.pdf','.png','.jpg','.jpeg','.gif']
class MainHandler(tornado.web.RequestHandler):
def get(self):
return self.render('index.html', supported_extensions = supported_extensions)
class UploadHandler(tornado.web.RequestHandler):
def post(self):
double_sided = True if self.get_argument('double-sided').lower() in ['true', '1', 'high'] else False #get double-sided URL argument
file = self.request.files.get('file') #get file from multipart data
if file == None:
self.set_status(400)
return self.finish('Missing file argument')
file = file[0]
filename = file.get('filename') #get filename
body = file.get('body') #get body
extension = os.path.splitext(filename)[1] #parse extension
if extension.lower() not in supported_extensions: #check if extension is supported
self.set_status(400)
return self.finish('Unsupported filetype')
fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6)) #generate random filename
final_filename = fname + extension
file_path = "./uploads/" + final_filename
with open(file_path, 'wb') as f:
f.write(file['body']) #write data to file
#generate lpr command options
lpr_args = ['lpr', '-r', '-o', 'media=A4', '-o', 'fit-to-page']
if double_sided:
lpr_args.append('-o')
lpr_args.append('sides=two-sided-long-edge')
else:
lpr_args.append('-o')
lpr_args.append('sides=one-sided')
lpr_args.append(file_path)
result = subprocess.run(lpr_args, stdout=subprocess.PIPE) #run lpr command
#print(file_path)
if result.stderr:
print("ERROR >%s<" % result.stderr, flush = True)
#os.remove(file_path)
finish_msg = "Printing %s%s, stdout >%s<, stderr >%s<" % (filename, ' Double-Sided' if double_sided else '', result.stdout, result.stderr)
print(finish_msg)
self.finish(finish_msg)
favicon_path = os.path.join(os.path.dirname(__file__), "static/img/favicon.ico")
static_path = os.path.join(os.path.dirname(__file__), "static")
templates_path = os.path.join(os.path.dirname(__file__), "templates")
settings = {
"template_path" : templates_path,
"static_path" : static_path,
"static_url_prefix" : "/s/",
"cookie_secret" : token_urlsafe(24), #token_urlsafe(24)
#"login_url" : "/login",
"debug" : DEBUG,
#"xsrf_cookies": True,
}
def make_app():
return tornado.web.Application([
(r'/(favicon\.ico)', tornado.web.StaticFileHandler, {'path': favicon_path }),
(r'/s/(.*)', tornado.web.StaticFileHandler, {'path': static_path }),
url(r"/", MainHandler, name = 'index'),
url(r"/home", MainHandler, name = 'home'),
url(r"/upload", UploadHandler, name = 'upload'),
], **settings)
if __name__ == '__main__':
app = make_app()
app.listen(HTTP_PORT)
print("Starting tornado server on port %d" % HTTP_PORT)
try:
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt:
print("Ending..")
print("Calling lprm")
subprocess.run(['lprm', '-']) #cancel all print jobs
subprocess.run(['lprm'])