简体   繁体   English

使用Python,为什么迭代sys.stdin永远不会退出并且form = cgi.FieldStorage()挂起?

[英]Using Python, why would iterating sys.stdin never exit and form = cgi.FieldStorage() hang?

I'm trying to write a webhook/callback python script to handle events raised by an email service provider (mailgun). 我正在尝试编写一个webhook / callback python脚本来处理由电子邮件服务提供商(mailgun)引发的事件。

MailGun provides a 'test' button to test my handler. MailGun提供了一个“测试”按钮来测试我的处理程序。 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). 当我这样做时,cgi.FieldStorage()是我的脚本进行的最后一次调用-并且未引发任何错误(它似乎挂起了)。

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? 问题 :是什么原因导致cgi.FieldStorage()似乎挂起,并且使sys.stdin似乎挂起?

I'm new to python - my debugging skills are weak :( 我是python的新手-我的调试技能很弱:(

Some more info: 更多信息:

I'm using windows 2008, iis 7.5 (?), python 3.3 cgi (C:\\Python33\\python.exe -u %s %s). 我正在使用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) 我正在使用IIS失败的请求日志记录(设置为日志状态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. 当mailgun不发送附件时,我使用cgi.FieldStorage()-可以正常工作。

Because you're debugging, you're attached to sys.stdin. 因为您正在调试,所以您已连接到sys.stdin。 cgi.FieldStorage will read from stdin if you don't pass it an fd. 如果您不通过fd,则cgi.FieldStorage将从stdin读取。

https://hg.python.org/cpython/file/54bd06097619/Lib/cgi.py#l482 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. 我猜它正在等待来自stdin的输入,并且可能不会出现EOF,可能是这些功能之一。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM