Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

windows - createNamedPipe in python

hello I have two python files(namedpipe)

a.py

import win32pipe, win32file

p = win32pipe.CreateNamedPipe(r'\.pipeest_pipe',
    win32pipe.PIPE_ACCESS_DUPLEX,
    win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_WAIT,
    1, 65536, 65536,300,None)

win32pipe.ConnectNamedPipe(p, None)


data = "Hello Pipe"  
win32file.WriteFile(p, data)

this pipe creates a namedpipe "test_pipe" and writes the data into the pipe.

b.py

import win32pipe, win32file
import win32file
fileHandle = win32file.CreateFile("\\.\pipe\test_pipe",
                              win32file.GENERIC_READ | win32file.GENERIC_WRITE,
                              0, None,
                              win32file.OPEN_EXISTING,
                              0, None)
data = win32file.ReadFile(fileHandle, 4096)
print data

i am able to get the data without any problem. but the data i get is in this form(i have enclosed the output in double quotes)

"<0, Hello Pipe>" 

I am unable to figure out why all these variables are being printed(<,0 ,>) where is the problem in a.py or b.py and how I can fix this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

That's just what win32file.ReadFile() returns. It returns a tuple of the result and the read data. You should verify the result is 0 (or anything else applicable) and then read the data.

data = win32file.ReadFile(fileHandle, 4096)
if data[0] == 0:
  print data[1]
else:
  print 'ERROR', data[0]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...