[python-win32] (no subject)

dan at shragmir.com dan at shragmir.com
Thu Jan 22 19:33:54 CET 2009


I am writing a simple client and server that communicate using a
duplex-mode named pipe on Windows.  For my initial effort, the server used
the Windows API calls to set up the pipe and communicate with it, but the
client used the regular Python file methods.  This works for exactly one
transaction.  That is

  --The client writes a message to the pipe
  --The server reads it
  --The server writes its response
  --The client reads the response.

But the second time around, I receive “IOError: [Errno 0] Error”  when the
client attempts to write the message.

This is not the end of the world, because I can write the client
application to use win32 API calls instead of the Python calls.  But does
anyone have any notion of why this is happening?  I am hoping the answer
may help me write clients in other applications (where I don’t have access
to the win32 API).

This code demonstrates the behavior:

import win32file
import win32pipe
import threading
import time
class Server( threading.Thread ):
    def run( self ):
        self.pipeHandle = win32pipe.CreateNamedPipe(
            '\\\\.\\pipe\\testpipe',
            win32pipe.PIPE_ACCESS_DUPLEX,
            win32pipe.PIPE_TYPE_BYTE |
            win32pipe.PIPE_READMODE_BYTE |
            win32pipe.PIPE_WAIT,
            50,
            4096,
            4096,
            10000,
            None)

        if self.pipeHandle == win32file.INVALID_HANDLE_VALUE:
            print 'Failed to create named pipe!'
            print 'Exiting...'
            sys.exit(1)

        win32pipe.ConnectNamedPipe( self.pipeHandle )
        while True:
            e, v = win32file.ReadFile( self.pipeHandle, 1, None )
            if v == 'A':
                print 'SERVER: Received request "%s"--answering' %v
                err, j = win32file.WriteFile( self.pipeHandle, 'B' )
                win32file.FlushFileBuffers( self.pipeHandle )
            else:
                print 'SERVER: Received request "%s"--exiting' %v
                break

        print "SERVER: Exiting server"

SERVER = Server()
SERVER.start()
time.sleep(0.1)
CLIENT_PIPE = open( '\\\\.\\pipe\\testpipe', 'a+b' )
for i in range( 10 ):
    CLIENT_PIPE.write( 'A' )
    CLIENT_PIPE.flush()
    reply = CLIENT_PIPE.read( 1 )
    print 'CLIENT: answer %d received: "%s"'%(i, reply)

CLIENT_PIPE.WRITE( 'C' )
CLIENT_PIPE.flush()



And the results:

SERVER: Received request "A"--answering
CLIENT: answer 0 received: "B"
Traceback (most recent call last):
  File
"C:\Python25\Lib\site-packages\pywin32-210.0001_s-py2.5-win32.egg\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Python25\Lib\site-packages\pyccf\simple_server_test.py", line
42, in <module>
    CLIENT_PIPE.write( 'A' )
IOError: [Errno 0] Error

--Dan Menes



More information about the python-win32 mailing list