首页 > 解决方案 > 在 Web 服务器中时无法从 Python 打印(到打印机)

问题描述

我可以使用此代码将作业发送到我的系统打印机:

lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE, close_fds=True)
print('printing from shell!')
lpr.stdin.write(b'print from python')
print('printed!')

但是,如果我在网络服务器中使用此代码,代码会运行,但打印机不会打印。

import http.server
import socketserver
import subprocess


def print_out(a,b,c):
  lpr = subprocess.Popen("/usr/bin/lpr", stdin=subprocess.PIPE, close_fds=True)
  print('printing from shell!')
  lpr.stdin.write(b'print from python')
  print('printed!')


PORT = 8080
with socketserver.TCPServer(("", PORT), print_out) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()

即使我print_out()在创建服务器(即上面PORT = 8080)之前尝试调用也是如此。

我怎样才能使这项工作?

标签: pythonprintingsubprocess

解决方案


推荐阅读