[英]Python and Windows Named Pipes
从Python在Windows上与命名管道进行通信的正确方法是什么? 我用谷歌搜索了它,找不到包装这种通信的任何软件包。
有:
我只需连接到现有的命名管道并读/写它。 我之前只尝试过使用串口进行通信(使用pySerial),我很惊讶与命名管道相比我能找到的信息很少。 对于Python来说,通常有大量的指南用于任何目的。
我会感激任何帮助。
为了连接到现有的命名管道,您可以使用通过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中)是尝试连接到管道的安全方法,直到服务器端创建它为止。
我成功地使用了以下片段。 此代码源自CaptureSetup / Pipes - Windows上的Python - Wireshark Wiki。 它需要pywin32
包中的win32pipe
和win32file
。
# 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%正确。
你提到了成为极客的危险和胜利:C#和Python之间的命名管道 -Jonathon Reinhart。 我试过了,但它无法创建命名管道。 我想知道该代码是否仅用于打开已由另一个进程创建的命名管道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.