Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python程序输出到命名管道_Python_Pipe_Output_Stdout_Named - Fatal编程技术网

Python程序输出到命名管道

Python程序输出到命名管道,python,pipe,output,stdout,named,Python,Pipe,Output,Stdout,Named,我使用一个程序(upx.exe)压缩和提取压缩的可执行文件。当我解压缩一个可执行文件时,唯一的选择就是写一个文件。我不希望提取的代码接触硬盘驱动器 因此,我考虑编写一个python包装器,启动可执行文件并从命名管道读取解压缩的代码。 我想 在python中创建命名管道 让外部程序将该管道用作输出文件,然后 将已写入该管道的内容读入字符串。 我尝试了以下方法: import win32pipe, win32file import win32file p = win32pipe.CreateNa

我使用一个程序(upx.exe)压缩和提取压缩的可执行文件。当我解压缩一个可执行文件时,唯一的选择就是写一个文件。我不希望提取的代码接触硬盘驱动器

因此,我考虑编写一个python包装器,启动可执行文件并从命名管道读取解压缩的代码。

我想

  • 在python中创建命名管道
  • 让外部程序将该管道用作输出文件,然后
  • 将已写入该管道的内容读入字符串。
  • 我尝试了以下方法:

    import win32pipe, win32file
    import win32file
    
    p = win32pipe.CreateNamedPipe(r'\\.\pipe\upx',
        win32pipe.PIPE_ACCESS_DUPLEX,
        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
        1, 65536, 65536,300,None)
    
    os.system("upx -d -o\\\\.\\pipe\\upx nbtscan-1.0.35.exe")
    
    win32pipe.ConnectNamedPipe(p, None)
    
    fileHandle = win32file.CreateFile(r'\\.\pipe\upx',
                                  win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                                  0, None,
                                  win32file.OPEN_EXISTING,
                                  0, None)
    
    
    data = win32file.ReadFile(fileHandle, 4096)
    print data
    
    情况就是这样:

    我没有使用命名管道的经验,现在感到非常绝望。 也许你有一个想法或教程给我举一些例子。(MSDN参考资料对我帮助不大)
    我的代码基于,但当我在“系统”调用之前使用“ConnectNamedPipe”函数时,程序只是挂起并等待,或者在该管道上执行某些操作
    请帮忙

    我是这样解决的:

    class Decompress(object):
    
        # Maximum to read from the decompressed stream
        maxsize = 2097152
        data = ""
    
        def __init__(self, application_path, type, filePath):
    
            # UPX Decompression
            if type == "UPX":
    
                print "Checking UPX file %s" % filePath 
    
                try:   
                    p = win32pipe.CreateNamedPipe(r'\\.\pipe\upx', win32pipe.PIPE_ACCESS_DUPLEX, win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT, 1, 2097152, 2097152, 300, None)
    
                    phandle = p.handle
    
                    success, stdout, stderr = run_popen_with_timeout(application_path + r'tools\upx.exe -d --no-color --no-progress --no-backup -o\\.\pipe\upx ' + filePath, 3, "")
                    #print stdout
                    #print stderr   
    
                    data = win32file.ReadFile(phandle, self.maxsize)
    
                    p.close()
    
                    #print "Read " + str(len(data[1])) + " bytes from decompressed EXE"
    
                    self.data = data[1]
                    #print self.data[:10]
    
                except Exception, e:
                    traceback.print_exc()
    
    def run_popen_with_timeout(command_string, timeout, input_data):
        """
        Run a sub-program in subprocess.Popen, pass it the input_data,
        kill it if the specified timeout has passed.
        returns a tuple of success, stdout, stderr
        """
        kill_check = threading.Event()
        def _kill_process_after_a_timeout(pid):
            os.kill(pid, signal.SIGTERM)
            kill_check.set() # tell the main routine that we had to kill
            # use SIGKILL if hard to kill...
            return
        p = Popen(command_string, stderr=STDOUT, stdout=PIPE)
        #print p.communicate()[0]
        print command_string
        pid = p.pid
        watchdog = threading.Timer(timeout, _kill_process_after_a_timeout, args=(pid, ))
        watchdog.start()
        (stdout, stderr) = p.communicate()
        watchdog.cancel() # if it's still waiting to run
        success = not kill_check.isSet()
        kill_check.clear()
        return (success, stdout, stderr)