Python和Windows命名管道

Ray*_* P. 10 python windows named-pipes python-3.x

从Python在Windows上与命名管道进行通信的正确方法是什么?我用谷歌搜索了它,找不到包装这种通信的任何软件包.

有:

我只需连接到现有的命名管道并读/写它.我之前只尝试过使用串口进行通信(使用pySerial),我很惊讶与命名管道相比我能找到的信息很少.对于Python来说,通常有大量的指南用于任何目的.

我会感激任何帮助.

Chr*_*Wue 14

为了连接到现有的命名管道,您可以使用CreateFile通过pywin32包提供的API .由于我花了一段时间将一个工作基地放在一起这是一个示例客户端/服务器,它适用于我(python 3.6.5,Windows 10 Pro x64上的pywin32 223):

import time
import sys
import win32pipe, win32file, pywintypes


def pipe_server():
    print("pipe server")
    count = 0
    pipe = win32pipe.CreateNamedPipe(
        r'\\.\pipe\Foo',
        win32pipe.PIPE_ACCESS_DUPLEX,
        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
        1, 65536, 65536,
        0,
        None)
    try:
        print("waiting for client")
        win32pipe.ConnectNamedPipe(pipe, None)
        print("got client")

        while count < 10:
            print(f"writing message {count}")
            # convert to bytes
            some_data = str.encode(f"{count}")
            win32file.WriteFile(pipe, some_data)
            time.sleep(1)
            count += 1

        print("finished now")
    finally:
        win32file.CloseHandle(pipe)


def pipe_client():
    print("pipe client")
    quit = False

    while not quit:
        try:
            handle = win32file.CreateFile(
                r'\\.\pipe\Foo',
                win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                0,
                None,
                win32file.OPEN_EXISTING,
                0,
                None
            )
            res = win32pipe.SetNamedPipeHandleState(handle, win32pipe.PIPE_READMODE_MESSAGE, None, None)
            if res == 0:
                print(f"SetNamedPipeHandleState return code: {res}")
            while True:
                resp = win32file.ReadFile(handle, 64*1024)
                print(f"message: {resp}")
        except pywintypes.error as e:
            if e.args[0] == 2:
                print("no pipe, trying again in a sec")
                time.sleep(1)
            elif e.args[0] == 109:
                print("broken pipe, bye bye")
                quit = True


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print("need s or c as argument")
    elif sys.argv[1] == "s":
        pipe_server()
    elif sys.argv[1] == "c":
        pipe_client()
    else:
        print(f"no can do: {sys.argv[1]}")
Run Code Online (Sandbox Code Playgroud)

示例输出客户端

> python pipe_test.py c
pipe client
no pipe, trying again in a sec
no pipe, trying again in a sec
no pipe, trying again in a sec
message: (0, b'0')
message: (0, b'1')
message: (0, b'2')
message: (0, b'3')
message: (0, b'4')
message: (0, b'5')
message: (0, b'6')
message: (0, b'7')
message: (0, b'8')
message: (0, b'9')
broken pipe, bye bye
Run Code Online (Sandbox Code Playgroud)

示例输出服务器

> python pipe_test.py s
pipe server
waiting for client
got client
writing message 0
writing message 1
writing message 2
writing message 3
writing message 4
writing message 5
writing message 6
writing message 7
writing message 8
writing message 9
finished now
Run Code Online (Sandbox Code Playgroud)

显然你需要对各种调用进行一些错误检查,但这应该有效.

附加说明:我的一位同事在客户端尝试对其执行I/O时关闭管道时遇到了麻烦(例外情况声称"所有管道实例都很忙").我发现他os.path.exists在客户端代码中使用它来测试命名管道在运行之前是否已经存在CreateFile.这不知何故打破了管道.因此,使用上面的方法(CreateFile包含在try-except中)是尝试连接到管道的安全方法,直到服务器端创建它为止.

  • 大!这只是管道示例,我可以在使用python 3+的Windows上运行 (2认同)

Cra*_*een 6

我成功地完成了类似以下片段的操作。此代码源自Windows 上的 CaptureSetup/Pipes \xe2\x80\x94 Python \xe2\x80\x94 The Wireshark Wiki。它需要win32pipewin32file来自pywin32包。

\n\n
# pipename should be of the form \\\\.\\pipe\\mypipename\npipe = win32pipe.CreateNamedPipe(\n        pipename,\n        win32pipe.PIPE_ACCESS_OUTBOUND,\n        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,\n        1, 65536, 65536,\n        300,\n        None)\ntry:\n    win32pipe.ConnectNamedPipe(pipe, None)\n\n    while True:\n        some_data = b\'12345...\'\n        win32file.WriteFile(pipe, some_data)\n        ...\nfinally:\n    win32file.CloseHandle(pipe)\n
Run Code Online (Sandbox Code Playgroud)\n\n

我不知道它关闭管道的方式是否100%正确。

\n\n

您提到了作为极客的危险和胜利:C# 和 Python 之间的命名管道\xe2\x80\x94Jonathon Reinhart。我尝试过,但无法创建命名管道。我想知道该代码是否只能打开已由另一个进程创建的命名管道。

\n