[python-win32] How to use win32file.TransmitFile with non blocking sockets?

Giampaolo Rodolà g.rodola at gmail.com
Thu Jan 27 23:11:57 CET 2011

Hi all,
I'm trying to take advantage of TransmitFile function in pyftpdlib:

I've noticed pywin32 provides a wrapper:
The example shown in there, though, assumes the socket is in blocking
state and the file gets sent entirely in a unique call.
I need to adapt that example to make it work with non-blocking sockets.
Specifically I want TransmitFile() and GetOverlappedResult() to return
immediately reporting the number of bytes sent.
The code below is what I managed to come up with so far but it
obviously doesn't work.
It either fails with "error: (996, 'GetOverlappedResult', 'Overlapped
I/O event is not in a signaled state.')" or the file delivered is
I don't have a clue on how should I use the overllaped structure,
neither it's clear to me what should I do to detect when EOF is

Any hint?

        def sendfile(sock, file, offset, nbytes):
            """Windows TranmistFile() wrapper, adapted to look like
            UNIX sendfile().
            ol = pywintypes.OVERLAPPED()
            ol.Offset = offset
            ol.hEvent = win32event.CreateEvent(None, 0, 0, None)
            filehandle = win32file._get_osfhandle(file)
            win32file.TransmitFile(sock, filehandle, nbytes, offset, ol, 0)
                sent = win32file.GetOverlappedResult(filehandle, ol, 0)
            except pywintypes.error, err:
                if err.args[0] == 38:  # EOF
                    return 0
                return sent

Thanks in advance,

--- Giampaolo

More information about the python-win32 mailing list