【问题标题】:Python and Windows Named PipesPython 和 Windows 命名管道
【发布时间】:2018-07-10 14:11:32
【问题描述】:

在 Windows 上通过 Python 与命名管道进行通信的正确方法是什么?我用谷歌搜索了它,但找不到任何包含此通信的包。

有:

我只需要连接到现有的命名管道并对其进行读/写。我以前只尝试过与串口通信(使用 pySerial),我很惊讶与它相比,我在命名管道上能找到的信息很少。通常有大量的 Python 指南。

我将不胜感激。

【问题讨论】:

  • 你应该可以使用f = open(r'\\.\PIPE\<pipe name>', 'rb+', buffering=0),然后调用f.read(nbytes)f.write(string)
  • @eryksun 您的建议与我列表中的第三个解决方案一致,除了它是“r+b”,并且您需要将字节写入文件,而不是字符串。我自己希望这是正确的方法,并且确实设法以这种方式与命名管道建立通信。我之前的错误是我以错误的方式发送了 ascii 控制代码。
  • "rb+" 和 "r+b" 是一样的。此外,您的问题被标记为 Python 2 并且没有提及 Python 3,所以我自然假设您使用的是 Python 2,其中 str 字符串是字节字符串。在 Python 3 中,您需要编写 bytes
  • 知道了。我没有专门为 Python 2 标记它,只是为 Python 标记。
  • 没错,Python 标记可以双向使用,但我仍然希望 Python 3 问题被标记为“Python-3.x”。心理上我想这是因为python 在大多数平台上仍然是 Python 2,而运行 Python 3 需要 python3

标签: python windows python-3.x named-pipes


【解决方案1】:

为了连接到现有的命名管道,您可以使用通过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 中)是尝试连接到管道直到它被服务器端创建的安全方法。

【讨论】:

  • 太棒了!这只是我可以找到在带有 python 3+ 的 Windows 上运行的管道示例
  • 您不必对使用win32file.CreateFile() 创建的文件调用一些CloseHandle() 吗?有没有办法用with 做到这一点?
  • @SergeRogatch 是的,您可能应该将相同的 finally 子句放入客户端,因为它已经存在于服务器中。
  • pip install pywin32 之后,在 Win8.1 和 Py3.7.6 上的工作就像魅力一样。
【解决方案2】:

我在下面的片段中取得了成功。此代码源自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)

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

您提到了The Perils and Triumphs of Being a Geek: Named Pipes between C# and Python—Jonathon Reinhart。我试过了,但它无法创建命名管道。我想知道该代码是否仅适用于打开已由另一个进程创建的命名管道。

【讨论】:

  • 有点晚但回复:C# 和 Python。 C# 代码是服务器,python 代码是客户端。在客户端启动之前,服务器必须正在运行并等待客户端。
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-16
相关资源
最近更新 更多