Question

I'm trying to write a webhook/callback python script to handle events raised by an email service provider (mailgun).

MailGun provides a 'test' button to test my handler. When they call my handler with an attachment, I encounter problems.

My first attempt was to use:

form = cgi.FieldStorage()

When I do this, cgi.FieldStorage() is the last call made by my script - and no errors are raised (it seems to hang).

I then attempted to use:

for line in sys.stdin:

Doing this helps - I get all of my data, but the loop never exits... it doesn't seem to be an infinite loop per say (it doesn't seem to be looping - I suspect it is trying to read that next line). My code after the loop never runs.

Question: What is causing cgi.FieldStorage() to seem to hang, and iterating sys.stdin to seem to hang?

I'm new to python - my debugging skills are weak :(

Some more info:

I'm using windows 2008, iis 7.5 (?), python 3.3 cgi (C:\Python33\python.exe -u %s %s).

I'm using iis's failed request logging (set up to log status 200)

Content-Type: multipart/form-data
Content-Length: 4225

This is some info about the attachment:

Content-Disposition: form-data; name="attachment-1"; filename="message.mime"
Content-Type: application/octet-stream
Content-Length: 1285

When mailgun doesn't send an attachment, I use cgi.FieldStorage() - and that works fine.

Was it helpful?

Solution

Because you're debugging, you're attached to sys.stdin. cgi.FieldStorage will read from stdin if you don't pass it an fd.

https://hg.python.org/cpython/file/54bd06097619/Lib/cgi.py#l482

I'm guessing it's waiting for input from stdin, and no EOF will occur, probably in one of these functions.

https://hg.python.org/cpython/file/54bd06097619/Lib/cgi.py#l775
https://hg.python.org/cpython/file/54bd06097619/Lib/cgi.py#l785

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top