Monday, January 24, 2005

ISAPI - How do you use ReadClient

While working on my ISAPI-WSGI implementation I needed to create a wrapper so i could emulate stdin. After a quick read of the Python isapi docs, I decided I needed to use the EXTENSION_CONTROL_BLOCK.ReadClient method. 30 minutes of blocked input and debugging, I started searching on Goggle for more info on ReadClient. It turns out that you only call ReadClient if ecb.TotalBytes is greater than ecb.AvailableBytes and that you must pass the value of difference to ReadClient else it blocks. So the code needed to be something like:


self._ecb = ecb
if self._ecb.AvailableBytes > 0:
data = self._ecb.AvailableData
# Check if more data from client than what is in
# ecb.AvailableData
excess = self._ecb.TotalBytes - self._ecb.AvailableBytes
if excess > 0:
extra = self._ecb.ReadClient(excess)
data = data + extra
self._in.write(data)
# rewind to start
self._in.seek(0)

Morale of story: win32 extension docs are light, due to it being a Python wrapper around the Windows API, and you need to read the Microsoft docs to get all the info.

Now just need to find some Microsoft docs that I can understand about Windows completion ports. Then I can start work on the threaded extension.

The simple ISAPI-WSGI handler seems to be working. Need to create some unit tests to exercise it.

CATEGORY:PYTHON

2 comments:

Andrey Lebedev said...

This turned to be very useful post indeed.

Thanks for figuring that out! :)

Anonymous said...

If you'd like to skip the Microsoft-specific parts, like ReadClient, check out PyISAPIe. It's a little less muddled and doesn't really require any win32 API specifics.

http://pyisapie.sourceforge.net/