-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_server.py
86 lines (50 loc) · 1.87 KB
/
print_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
import os
import json
import time
import threading
from flask import Flask, request
from werkzeug.utils import secure_filename
from redis import StrictRedis
from print_lib import print_file
REDIS_CONNECTION = StrictRedis(host=json.loads(open("config/config.json", "r").read())["REDIS_SERVER"])
UPLOAD_FOLDER = json.loads(open("config/config.json", "r").read())["SERVER"]["PRINT_PATH"]
ALLOWED_EXTENSIONS = set(["jpg", "txt", "pdf", "xls", "xlsx", "doc", "docx"])
APP = Flask(__name__)
APP.config["UPLOAD_FOLDER"] = UPLOAD_FOLDER
def print_engine(redis_conn):
while True:
print_file(redis_conn)
time.sleep(5)
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def gen_error(error_msg):
return json.dumps({"Status": "Error", "Error_Msg": error_msg})
@APP.route('/')
def front_page():
return "HELLO WORLD!"
@APP.route("/upload", methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
return gen_error("No File part")
file = request.files['file']
if file.filename == '':
return gen_error("No selected file")
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(APP.config["UPLOAD_FOLDER"], filename))
return json.dumps(
{
"Status": "Success", "File_Uploaded": filename,
"Job_status": "Sent for printing"
}
)
return gen_error("Invalid Action")
def main():
print_thread = threading.Thread(target=print_engine, args=(REDIS_CONNECTION,))
print_thread.setDaemon(True)
print_thread.start()
APP.run(host='0.0.0.0', debug=True, threaded=True)
if __name__ == '__main__':
main()