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

Roger Upole rupole at hotmail.com
Fri Jan 28 14:01:52 CET 2011


Giampaolo Rodolà wrote:
> Hi all,
> I'm trying to take advantage of TransmitFile function in pyftpdlib:
> http://code.google.com/p/pyftpdlib/issues/detail?id=152
>
> I've noticed pywin32 provides a wrapper:
> http://sourceforge.net/tracker/index.php?func=detail&aid=1962146&group_id=78018&atid=551956
> 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
> corrupted.
> 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
> reached.
>
> 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)
>           try:
>               sent = win32file.GetOverlappedResult(filehandle, ol, 0)
>           except pywintypes.error, err:
>               if err.args[0] == 38:  # EOF
>                   return 0
>               raise
>           else:
>               return sent
>
>
> Thanks in advance,
>
> --- Giampaolo

The third arg to GetOverlappedResult should be True to wait for an
async operation to complete.

          Roger






More information about the python-win32 mailing list