一、前言
在此,我以python3.11为例,其他版本也都大同小异。
我们需要一些库:
import os
from bottle import route, request, static_file, run
这其中,没有的自行使用pip安装即可。
二、建造服务器
为了图省事,我使用的是bottle库。
本文我只讲bottle库的上传的方法,其他功能您可以在网上查询到。
接下来,正式开始建设服务器。
首先,您需要知道一些有关HTML的知识,在此,我默认您已经知道一些。
首先,先写一个html界面,作为我们服务器的主界面。
mian.html :
<html>
<head>
<title>打印</title>
</head>
<body>
<div>
<form action="/upload" method="post" enctype="multipart/form-data">
照片:<input type="file" name="upload" /><br/>
<input type="submit" value="开始上传" />
</form>
</div>
</body>
</html>
这就是我们的前端部分。注意:form是表单,规定了我们使用后端哪些命令。
接下来是后端部分:
run(host= 192.168.0.103, port=8080)#前面是ip地址,可以写自己电脑的ip,此处为一个示例,后面是端口号,规定从哪个端口访问,一般浏览器默认为80
这时往浏览器中输入http://192.168.0.103/8080便可以登录自己的服务器了。
但是,有一个问题,就是浏览器会报错,为什么呢?翻译之后会明白,所指向的位置没有网页!!!
所以,首先我们需要在/处添加一个网页:
#当网址最后是/时返回main.html
@route('/')
def root():
return static_file('main.html', root='.')
这时刷新网页就可以看到网页了。
但是,此时上传文件也会报错,因为/upload还没有规定,所以,接下来规定/upload:
@route('/upload', method='POST')
def do_upload():
upload = request.files.get('upload')
save_path = "D:\下载"
if not os.path.exists(save_path):
os.makedirs(save_path)
printimg.printimg()
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "成功上传至服务器 '{0}'".format(save_path)
这时,如果你也用VS Code 你会发现printimg()未被定义,所以接下来就是定义printimg():
printimg.py:
import os
import win32print
import win32ui
from PIL import Image, ImageWin
def printimg():
print("正在打印")
path1 = 'D:\下载' # 输入文件夹地址
files = os.listdir(path1) # 读入文件夹
#打印图片
for filename in files:
filen = "下载/" + filename
HORZRES = 8
VERTRES = 10
LOGPIXELSX = 88
LOGPIXELSY = 90
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = filen
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
print("已打印一项")
os.remove(filen)#删除文件,防止重复打印
在这里,我的方法是自己规定一个库,再导入我自己的库的函数,当然,也可以只写一个文件:
import os
from bottle import route, request, static_file, run
import win32print
import win32ui
from PIL import Image, ImageWin
def printimg():
print("正在打印")
path1 = 'D:\下载' # 输入文件夹地址
files = os.listdir(path1) # 读入文件夹
#打印图片
for filename in files:
filen = "下载/" + filename
HORZRES = 8
VERTRES = 10
LOGPIXELSX = 88
LOGPIXELSY = 90
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = filen
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
print("已打印一项")
os.remove(filen)#删除文件,防止重复打印
@route('/upload', method='POST')
def do_upload():
upload = request.files.get('upload')
save_path = "D:\下载"
if not os.path.exists(save_path):
os.makedirs(save_path)
printimg()
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "成功上传至服务器 '{0}'".format(save_path)
到现在,上传和打印功能就都完成了。
但是,实际使用时,因为我不想更改WIFI为静态ip,而不使用本机ip可能会出问题,所以我在代码中加入了自动读取ip的函数,这就需要一个新的库:socket
import socket
#获取计算机名称
hostname=socket.gethostname()
#获取本机IP
ip=socket.gethostbyname(hostname)
所以,run函数的host就可以改为:
run(host= ip, port=8080)
三、完整代码
(一)多个.py文件和一个.html文件
main.py :
import os
from bottle import route, request, static_file, run
import socket
import printimg
#获取计算机名称
hostname=socket.gethostname()
#获取本机IP
ip=socket.gethostbyname(hostname)
@route('/')
def root():
return static_file('main.html', root='.')
@route('/upload', method='POST')
def do_upload():
upload = request.files.get('upload')
save_path = "D:\下载"
if not os.path.exists(save_path):
os.makedirs(save_path)
printimg.printimg()
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "成功上传至服务器 '{0}'".format(save_path)
if __name__ == '__main__':
run(host= ip, port=8080)
main.html :
<html>
<head>
<title>打印</title>
</head>
<body>
<div>
<form action="/upload" method="post" enctype="multipart/form-data">
照片:<input type="file" name="upload" /><br/>
<input type="submit" value="开始上传" />
</form>
</div>
</body>
</html>
printimg.py :
import os
import win32print
import win32ui
from PIL import Image, ImageWin
def printimg():
print("正在打印")
path1 = 'D:\下载'
files = os.listdir(path1)
#打印图片
for filename in files:
filen = "下载/" + filename
HORZRES = 8
VERTRES = 10
LOGPIXELSX = 88
LOGPIXELSY = 90
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = filen
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
print("已打印一项")
os.remove(filen)
(二)一个.py文件和一个.html文件
main.py :
import os
from bottle import route, request, static_file, run
import socket
import win32print
import win32ui
from PIL import Image, ImageWin
def printimg():
print("正在打印")
path1 = 'D:\下载'
files = os.listdir(path1)
#打印图片
for filename in files:
filen = "下载/" + filename
HORZRES = 8
VERTRES = 10
LOGPIXELSX = 88
LOGPIXELSY = 90
PHYSICALWIDTH = 110
PHYSICALHEIGHT = 111
PHYSICALOFFSETX = 112
PHYSICALOFFSETY = 113
printer_name = win32print.GetDefaultPrinter ()
file_name = filen
hDC = win32ui.CreateDC ()
hDC.CreatePrinterDC (printer_name)
printable_area = hDC.GetDeviceCaps (HORZRES), hDC.GetDeviceCaps (VERTRES)
printer_size = hDC.GetDeviceCaps (PHYSICALWIDTH), hDC.GetDeviceCaps (PHYSICALHEIGHT)
printer_margins = hDC.GetDeviceCaps (PHYSICALOFFSETX), hDC.GetDeviceCaps (PHYSICALOFFSETY)
bmp = Image.open (file_name)
if bmp.size[0] > bmp.size[1]:
bmp = bmp.rotate (90)
ratios = [1.0 * printable_area[0] / bmp.size[0], 1.0 * printable_area[1] / bmp.size[1]]
scale = min (ratios)
hDC.StartDoc (file_name)
hDC.StartPage ()
dib = ImageWin.Dib (bmp)
scaled_width, scaled_height = [int (scale * i) for i in bmp.size]
x1 = int ((printer_size[0] - scaled_width) / 2)
y1 = int ((printer_size[1] - scaled_height) / 2)
x2 = x1 + scaled_width
y2 = y1 + scaled_height
dib.draw (hDC.GetHandleOutput (), (x1, y1, x2, y2))
hDC.EndPage ()
hDC.EndDoc ()
hDC.DeleteDC ()
print("已打印一项")
os.remove(filen)
#获取计算机名称
hostname=socket.gethostname()
#获取本机IP
ip=socket.gethostbyname(hostname)
@route('/')
def root():
return static_file('main.html', root='.')
@route('/upload', method='POST')
def do_upload():
upload = request.files.get('upload')
save_path = "D:\下载"
if not os.path.exists(save_path):
os.makedirs(save_path)
printimg.printimg()
file_path = "{path}/{file}".format(path=save_path, file=upload.filename)
upload.save(file_path)
return "成功上传至服务器 '{0}'".format(save_path)
if __name__ == '__main__':
run(host= ip, port=8080)