[python-win32] overlaped namedpipe client with CreateFile instead of CallNamedPipe

mani sabri mani.sabri at gmail.com
Sat Jun 28 18:03:28 CEST 2008


Hi 

In M.Hammonds "python programming on win32" chapter 18 there is a pipe
service and a client [1] which use CallNamedPipe. I changed the service to
accept messages bigger than its buffer [2]. Does anybody know how to rewrite
the client with CreateFile method so it can accept bigger that buffer
messages too? 
My attempt [3] which mimics
http://msdn.microsoft.com/en-us/library/aa365592(VS.85).aspx fails with
following message:
pywintypes.error: (121, 'WaitNamedPipe', 'The semaphore timeout period has
expired.')

Best regards,
Mani 



[1] --------------------------------------------------------------
# PipeServiceClient.py
#
# A client for testing the PipeService.
#
# Usage:
#
#   PipeServiceClient.py message

import win32pipe
import sys
import string

if __name__=='__main__':
    message = string.join(sys.argv[1:])
    pipeName = "\\\\.\\pipe\\PyPipeService"
    data = win32pipe.CallNamedPipe(pipeName, message, 512, 0)
    print "The service sent back:"
    print data


[2]--------------------------------------------------------------
hr, data = win32file.ReadFile(pipeHandle, 256)
	if (win32api.GetLastError() == winerror.ERROR_MORE_DATA):
		t,t,remains = win32pipe.PeekNamedPipe(pipeHandle,0)
		hr,d2 = win32file.ReadFile(pipeHandle, remains)
		data = data + d2


[3]--------------------------------------------------------------
import win32pipe
import winerror
import pywintypes
import win32file
import win32security
import cPickle as pickle
from cPickle import HIGHEST_PROTOCOL

if __name__=='__main__':
	message = pickle.dumps(range(1000),HIGHEST_PROTOCOL)    
	pipeName = "\\\\.\\pipe\\pyPipe"        
	
	saAttr = win32security.SECURITY_ATTRIBUTES()
	saAttr.SetSecurityDescriptorDacl ( 1, None, 0 )
	
	pipeHandle = win32file.CreateFile(pipeName,
		win32file.GENERIC_READ | win32file.GENERIC_WRITE,
		0,
		None,
		win32file.OPEN_EXISTING,
	
win32file.FILE_FLAG_OVERLAPPED|win32file.FILE_ATTRIBUTE_NORMAL,
		None)
	
	win32pipe.WaitNamedPipe(pipeName,2000)  	
	
	win32pipe.SetNamedPipeHandleState(pipeHandle,
win32pipe.PIPE_READMODE_MESSAGE,
				None,
				None)
	
	win32file.WriteFile(pipeHandle, message)
	hr, data = win32file.ReadFile(pipeHandle, 256)
	
	#TODO: read the remains of the message
	
	print 'len(data):',len(data)
	print "The service sent back:"
	print pickle.loads(data)
	
	



More information about the python-win32 mailing list