Python / perl打印服务器;将打印作业写入文件

时间:2012-04-26 14:47:12

标签: python printing asyncore

我正在尝试使用此功能运行打印服务器:http://newcenturycomputers.net/projects/rawprintserver.html

我已经修改了spooler.py来写一个文件,一切正常。除了一个问题:考虑到正在打印的内容,作业似乎需要时间...

这份工作得到了收到,并且在实际完成之前它已经开始了一段时间。这似乎是一个1kb txt文件的过长时间,其中包含' test'在它...

无论是使用Windows还是Mac,它都表现出相同的行为,因此它不是操作系统的东西...

这是spooler.py中修改后的def

class printer(base_printer):
    def sendjob(self, fp, title = None):
        # title is irrelevant here
        print "Begin sendjob def..."
        out = open('/tmp/printserv/outs/' + str(time.time()).split('.')[0]+".txt", 'w')
        blk = fp.read(8192)
        while blk:
            out.write(blk)
            print "Block written..."
            blk = fp.read(8192)
            print "while loop done..."
        rc = out.close()
        if rc is not None:
            print "Error: lpr returns %02x" % rc
        else:
            print "OK DONE"

这是处理作业的主要打印处理程序类(来自printserver.py):

class print_handler(asyncore.dispatcher):

    def __init__(self, conn, addr, server, jobnumber):
        asyncore.dispatcher.__init__(self, sock = conn)
        self.addr = addr
        self.server = server
        self.jobname = JOBNAME % jobnumber
        self.fp = open(self.jobname, "wb")
        print "Receiving Job from %s for Printer <%s> (Spool File %s)" \
            % (addr, self.server.printer.printer_name, self.jobname)

    def handle_read(self):
        data = self.recv(8192)
        print "Starting handle_read"
        if self.fp:
            print "begin self.fp.write..."
            self.fp.write(data)
            print "end self.fp.write.."

    def writable(self):
        print "Writable return 0"
        return 0

    def handle_write(self):
        print "handle_write pass"
        pass

    def handle_close(self):
        print "Printer <%s>: Printing Job %s" \
            % (self.server.printer.printer_name, self.jobname)
        if self.fp:
            self.fp.close()
            self.fp = None
        fp = open(self.jobname, "rb")
        self.server.printer.sendjob(fp)
        fp.close()
        try:
            os.remove(self.jobname)
        except:
            print "Can't Remove <%s>" % self.jobname
        self.close()

和mainloop def(也在printserver.py中):

def mainloop(config):

    if config["spooldir"]:
        os.chdir(config["spooldir"])

    for i in range(len(config["printer"])):
        args = string.split(config["printer"][i], ",", maxsplit = 1)
        prn = args[1].strip()
        port = int(args[0].strip())
        p = print_server('',
            port, spooler.printer(prn))
        servers.append(p)

    try:
        try:
            asyncore.loop(timeout = 1.0)
        except KeyboardInterrupt:
            pass
    finally:
        print "Print Server Exit"

那些print被记录在一个如下所示的日志文件中:

[2012/04/26 10:39:58] Raw Print Server Startup: PID = 15695
[2012/04/26 10:39:58] Starting Printer <lpr> on port 515
[2012/04/26 10:40:01] Receiving Job from ('1.2.3.4', 55070) for Printer <lpr> (Spool File RawPrintJob00001.prn)
[2012/04/26 10:40:01] Writable return 0
[2012/04/26 10:40:01] Starting handle_read
[2012/04/26 10:40:01] begin self.fp.write...
[2012/04/26 10:40:01] end self.fp.write..
[2012/04/26 10:40:01] Writable return 0
[2012/04/26 10:40:02] Writable return 0
[2012/04/26 10:40:03] Writable return 0
[2012/04/26 10:40:04] Writable return 0
                 [[SNIP]]
[2012/04/26 10:41:39] Writable return 0
[2012/04/26 10:41:40] Writable return 0
[2012/04/26 10:41:41] Writable return 0
[2012/04/26 10:41:42] Starting handle_read
[2012/04/26 10:41:42] begin self.fp.write...
[2012/04/26 10:41:42] end self.fp.write..
[2012/04/26 10:41:42] Writable return 0
[2012/04/26 10:41:42] Printer <lpr>: Printing Job RawPrintJob00001.prn
[2012/04/26 10:41:42] Begin sendjob def...
[2012/04/26 10:41:42] Block written...
[2012/04/26 10:41:42] done writing blocks...
[2012/04/26 10:41:42] OK DONE
[2012/04/26 10:41:42] Starting handle_read

此时,作业已完成,文件已写入。在下一份工作进入之前,没有其他任何东西被记录下来。正如你所看到的,这需要1.5分钟才能完成。

我的问题是:处理1k文件怎么可能花费近2分钟?这在千兆局域网上都是本地的,所以它必须与代码处理工作的方式有关...我注意到的一件事是打印的工作似乎有很多尾随的空白。可能是因为它正在寻找EOF或其他什么东西,最终只是超时?

赞赏任何指针

1 个答案:

答案 0 :(得分:0)

原来这是关于如何在客户端上配置打印机的问题。它被设置为lpr而不是RAW。

巧合的是,我遇到了一个perl脚本,它将与py脚本(有点)做同样的事情。使用perl脚本,实现相同目标的代码要少得多(我的目标是简单地捕获流并写入文件; rps比我需要的更冗长)。

下面:

#!/usr/bin/perl 
use IO::Socket::INET;
use POSIX qw(strftime);
$print_time = strftime "%Y%m%d-%H_%M_%S", localtime; 
$myport=515; 
$pserve=IO::Socket::INET->new(LocalPort => $myport,Type=>SOCK_STREAM,Reuse=>1,Listen=>1) or die "Socket error: $!\n"; 
while ($pjob=$pserve->accept()) {
    open(J,">>/tmp/printserv/outs/".$print_time.".txt") or print "File open error: $!\n"; 
    while (<$pjob>) { 
        print J "$_"; 
    } 
    close J; 
    close $pjob;
    print  "DONE..."; 
}