コード例 #1
0
ファイル: test_printer.py プロジェクト: wolfking2/miniprint
def test_fsdownload():
    p = Printer(logger)
    c = b'FSDOWNLOAD FORMAT:BINARY SIZE=52 NAME="0:/test2.txt"\r\nthis is a file with only one line and no line breaks\r\n'
    r = p.command_fsdownload(c.decode('UTF-8'))
    assert r == ''

    r = p.command_fsquery('@PJL FSQUERY NAME="0:/test2.txt"')
    assert r == '@PJL FSQUERY NAME="0:/test2.txt" TYPE=FILE SIZE=52'

    c = b'FSDOWNLOAD FORMAT:BINARY SIZE=77 NAME="0:/test2.txt"\r\nthis is a file with 2 lines\nhere is the second line with no ending line break\r\n'
    r = p.command_fsdownload(c.decode('UTF-8'))
    assert r == ''

    r = p.command_fsquery('@PJL FSQUERY NAME="0:/test2.txt"')
    assert r == '@PJL FSQUERY NAME="0:/test2.txt" TYPE=FILE SIZE=77'
コード例 #2
0
    def handle(self):
        hpclient = hpfeeds.new(HPF_HOST, HPF_PORT, HPF_IDENT, HPF_SECRET)
        # self.request is the TCP socket connected to the client
        source_ip = self.client_address[0]
        source_port = self.client_address[1]
        dest_ip = socket.gethostbyname(socket.gethostname())
        dest_port = self.server.server_address[1]
        printer = Printer(logger, source_ip)
        logger.info("handle - open_conn - " + source_ip + ":" + str(source_port))
        printer.events_list.append("handle - open_conn - " + source_ip + ":" + str(source_port))

        emptyRequest = False
        while emptyRequest == False:

            # Wait a maximum of conn_timeout seconds for another request
            # If conn_timeout elapses without request, close the connection
            ready = select.select([self.request], [], [], conn_timeout)
            if not ready[0]:
                break

            try:
                self.data = self.request.recv(1024).strip()
            except Exception as e:
                logger.error("handle - receive - Error receiving data - possible port scan")
                printer.events_list.append("handle - receive - Error receiving data - possible port scan")
                emptyRequest = True
                break

            request = self.data.decode('UTF-8')
            request = request.replace('\x1b%-12345X', '')

            if request[0:2] == '%!':
                printer.receiving_postscript = True
                printer.postscript_data = request

                logger.info('handle - postscript - Received first postscript request of file')
                printer.events_list.append('handle - postscript - Received first postscript request of file')

                continue
            elif printer.receiving_postscript:
                printer.postscript_data += request

                if '%%EOF' in request:
                    printer.save_postscript()
                    printer.receiving_postscript = False

                continue
            
            commands = self.parse_commands(request)

            logger.debug('handle - request - ' + str(request.encode('UTF-8')))
            printer.events_list.append('handle - request - ' + str(request.encode('UTF-8')))

            if len(commands) == 0:  # If we're sent an empty request, close the connection
                emptyRequest = True
                break

            try:
                response = ''

                for command in commands:
                    command = command.lstrip()
                    
                    if command.startswith("@PJL "):
                        command = command[5:]
                        if printer.printing_raw_job:
                            printer.save_raw_print_job()

                        if command.startswith("ECHO"):
                            response += printer.command_echo(command)
                        elif command.startswith("USTATUSOFF"):
                            response += printer.command_ustatusoff(command)
                        elif command.startswith("INFO ID"):
                            response += printer.command_info_id(command)
                        elif command.startswith("INFO STATUS"):
                            response += printer.command_info_status(command)
                        elif command.startswith("FSDIRLIST"):
                            response += printer.command_fsdirlist(command)
                        elif command.startswith("FSQUERY"):
                            response += printer.command_fsquery(command)
                        elif command.startswith("FSMKDIR"):
                            response += printer.command_fsmkdir(command)
                        elif command.startswith("FSUPLOAD"):
                            response += printer.command_fsupload(command)
                        elif command.startswith("FSDOWNLOAD"):
                            response += printer.command_fsdownload(command)
                        elif command.startswith("RDYMSG"):
                            response += printer.command_rdymsg(command)
                        else:
                            logger.error("handle - cmd_unknown - " + str(command))
                            printer.events_list.append("handle - cmd_unknown - " + str(command))
                    else:
                        response += printer.append_raw_print_job(command)

                logger.info("handle - response - " + str(response.encode('UTF-8')))
                printer.events_list.append("handle - response - " + str(response.encode('UTF-8')))
                self.request.sendall(response.encode('UTF-8'))

            except Exception as e:
                tb = sys.exc_info()[2]
                traceback.print_tb(tb)
                logger.error("handle - error_caught - " + str(e))
                printer.events_list.append("handle - error_caught - " + str(e))

        if printer.printing_raw_job:
            printer.save_raw_print_job()
        logger.info("handle - close_conn - " + source_ip + ":" + str(source_port))
        printer.events_list.append("handle - close_conn - " + source_ip + ":" + str(source_port))
        now = datetime.now()
        events_json = {"source_ip": source_ip, "source_port" : source_port, "dest_ip": dest_ip, "dest_port": dest_port,"timestamp": datetime.timestamp(now), "events": printer.events_list}
        hpclient.publish(HPF_CHAN, json.dumps(events_json))
コード例 #3
0
ファイル: server.py プロジェクト: wolfking2/miniprint
    def handle(self):
        # self.request is the TCP socket connected to the client
        logger.info("handle - open_conn - " + self.client_address[0])
        printer = Printer(logger)

        emptyRequest = False
        while emptyRequest == False:

            # Wait a maximum of conn_timeout seconds for another request
            # If conn_timeout elapses without request, close the connection
            ready = select.select([self.request], [], [], conn_timeout)
            if not ready[0]:
                break

            try:
                self.data = self.request.recv(1024).strip()
            except Exception as e:
                logger.error(
                    "handle - receive - Error receiving data - possible port scan"
                )
                emptyRequest = True
                break

            request = self.data.decode('UTF-8')
            request = request.replace('\x1b%-12345X', '')

            if request[0:2] == '%!':
                printer.receiving_postscript = True
                printer.postscript_data = request

                logger.info(
                    'handle - postscript - Received first postscript request of file'
                )

                continue
            elif printer.receiving_postscript:
                printer.postscript_data += request

                if '%%EOF' in request:
                    printer.save_postscript()
                    printer.receiving_postscript = False

                continue

            commands = self.parse_commands(request)

            logger.debug('handle - request - ' + str(request.encode('UTF-8')))

            if len(
                    commands
            ) == 0:  # If we're sent an empty request, close the connection
                emptyRequest = True
                break

            try:
                response = ''

                for command in commands:
                    command = command.lstrip()

                    if command.startswith("@PJL "):
                        command = command[5:]
                        if printer.printing_raw_job:
                            printer.save_raw_print_job()

                        if command.startswith("ECHO"):
                            response += printer.command_echo(command)
                        elif command.startswith("USTATUSOFF"):
                            response += printer.command_ustatusoff(command)
                        elif command.startswith("INFO ID"):
                            response += printer.command_info_id(command)
                        elif command.startswith("INFO STATUS"):
                            response += printer.command_info_status(command)
                        elif command.startswith("FSDIRLIST"):
                            response += printer.command_fsdirlist(command)
                        elif command.startswith("FSQUERY"):
                            response += printer.command_fsquery(command)
                        elif command.startswith("FSMKDIR"):
                            response += printer.command_fsmkdir(command)
                        elif command.startswith("FSUPLOAD"):
                            response += printer.command_fsupload(command)
                        elif command.startswith("FSDOWNLOAD"):
                            response += printer.command_fsdownload(command)
                        elif command.startswith("RDYMSG"):
                            response += printer.command_rdymsg(command)
                        else:
                            logger.error("handle - cmd_unknown - " +
                                         str(command))
                    else:
                        response += printer.append_raw_print_job(command)

                logger.info("handle - response - " +
                            str(response.encode('UTF-8')))
                self.request.sendall(response.encode('UTF-8'))

            except Exception as e:
                tb = sys.exc_info()[2]
                traceback.print_tb(tb)
                logger.error("handle - error_caught - " + str(e))

        if printer.printing_raw_job:
            printer.save_raw_print_job()
        logger.info("handle - close_conn - " + self.client_address[0])
コード例 #4
0
ファイル: test_printer.py プロジェクト: wolfking2/miniprint
def test_fsquery():
    p = Printer(logger)
    r = p.command_fsquery('@PJL FSQUERY NAME="0:/webServer"')
    assert r == '@PJL FSQUERY NAME="0:/webServer" TYPE=DIR'