Python和Windows命名管道

48

在Windows中,使用Python与命名管道进行通信的正确方法是什么?我已经搜索过了,找不到任何包可以包装这种通信。

有以下内容:

我只需要连接到一个现有命名管道并读写它。以前我只尝试过使用串口进行通信(使用pySerial),我很惊讶相比之下我找到的关于命名管道的信息很少。对于Python,通常有大量用途指南。

我将感激任何帮助。


1
你应该能够使用 f = open(r'\\.\PIPE\<pipe name>', 'rb+', buffering=0),然后调用 f.read(nbytes)f.write(string) - Eryk Sun
1
@eryksun,您的建议与我列表中的第三个解决方案类似,只是它是“r+b”,而您需要将字节写入文件,而不是字符串。我自己也希望这是正确的方法,并且确实成功地通过命名管道建立了通信。我以前的错误是错误地发送ASCII控制代码。 - Ray P.
"rb+"和"r+b"是相同的。此外,您的问题标记为Python 2,并没有提到Python 3,因此我自然而然地假设您正在使用Python 2,在其中str字符串是字节字符串。在Python 3中,您需要编写bytes - Eryk Sun
明白了。我没有特别标记它是针对Python 2的,只是针对Python。 - Ray P.
没错,Python标签可以双向使用,但我仍然期望Python 3的问题被标记为“Python-3.x”。在心理上,我认为这是因为大多数平台上的python仍然是Python 2,而运行Python 3需要使用python3 - Eryk Sun
2个回答

59

要连接到现有的命名管道,您可以利用通过pywin32包提供的CreateFile 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]}")

实例输出客户端

> 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

示例输出服务器

> 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

显然,您需要在各种调用周围进行一些错误检查,但那应该可以工作。

额外的附注:我的一个同事在客户端尝试对其执行I/O时遇到了管道被关闭的问题(异常声称“所有管道实例都很忙”)。后来发现他在客户端代码中使用os.path.exists测试命名管道是否已经存在,然后再运行CreateFile。这会破坏管道。因此,使用上述方法(将CreateFile包装在try-except中)是安全的方法,直到服务器端创建了它。


6
太好了!这是我在Windows上找到的唯一一个可以在Python 3+上运行的管道示例。 - Finn
你不需要在使用win32file.CreateFile()创建的文件上调用CloseHandle()吗?有没有一种使用with的方法来实现这个? - Serge Rogatch
1
@SergeRogatch 是的,你应该将相同的 finally 子句放入客户端,因为它已经存在于服务器端。 - ChrisWue
在Win8.1和Py3.7.6上运行良好,只需执行pip install pywin32即可。 - not2qubit

6
我对以下片段的代码有所成功。这段代码源自于CaptureSetup/Pipes — Python on Windows ——Wireshark Wiki。它需要来自pywin32包的win32pipewin32file
# pipename should be of the form \\.\pipe\mypipename
pipe = win32pipe.CreateNamedPipe(
        pipename,
        win32pipe.PIPE_ACCESS_OUTBOUND,
        win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
        1, 65536, 65536,
        300,
        None)
try:
    win32pipe.ConnectNamedPipe(pipe, None)

    while True:
        some_data = b'12345...'
        win32file.WriteFile(pipe, some_data)
        ...
finally:
    win32file.CloseHandle(pipe)

我不确定它在关闭管道的方式上是否完全正确。
你提到了《成为极客的危险和胜利:C#和Python之间的命名管道》- Jonathon Reinhart。我尝试了它,但无法创建命名管道。我想知道那段代码是否只适用于打开已由另一个进程创建的命名管道。

1
有点晚了,但关于C#和Python。C#代码是服务器,Python代码是客户端。在客户端启动之前,服务器必须运行并等待客户端。 - cup

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接