-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcat.py
executable file
·52 lines (43 loc) · 1.42 KB
/
webcat.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
#!/usr/bin/env python3
# webcat.py
# K0RNH0LI0 2021
#
# Web interface for printing to BlueTooth thermal cat printers.
import os
import asyncio
from flask import Flask, request, render_template, flash, redirect
from subprocess import Popen, PIPE, STDOUT
import config
import driver
app = Flask(__name__)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/print", methods=["GET", "POST"])
def catprint():
if request.method == "POST":
if os.path.exists("image.pbm"):
flash("Printer is busy 3:")
return redirect("/")
if not "file" in request.files:
flash("Please upload a file 3:")
return redirect("/")
# convert file to P4 PBM image
file = request.files["file"].read()
p = Popen(["convert", "-", "-resize", "384x", "-monochrome", "pbm:-"],
stdout=PIPE,
stdin=PIPE,
stderr=PIPE)
convert = p.communicate(input=file)
if convert[1] != b'':
# error converting file
flash("Please upload a valid image 3:")
return redirect("/")
with open("image.pbm", "wb") as f:
f.write(convert[0])
flash("Image sent to printer :3")
return redirect("/")
if __name__ == "__main__":
app.secret_key = config.SECRET_KEY
app.config["MAX_CONTENT_LENGTH"] = config.SIZE_LIMIT
app.run("0.0.0.0", config.HTTP_PORT)