我正在用 python 编写一个简单的脚本重放保存的 HTTP 请求 https://stackoverflow.com/questions/8384848/method-program-for-sending-a-given-http-request-with-headers.
这是脚本:
import httplib
requestFileName = 'C:/Users/Owner/request.txt'
connectionURL = 'localhost:4059'
requestFile = open(requestFileName,'r')
connection = httplib.HTTPConnection(connectionURL)
connection.send(requestFile.read())
response = connection.getresponse()
print response.status + " " + response.reason
connection.close()
这是request.txt
:
POST /ipn/handler.ashx?inst=272&msgType=result HTTP/1.0
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Host: mysite.com
Content-Length: 28
User-Agent: AGENT/1.0 (UserAgent)
region=website&compName=ACTL
当我运行此脚本时,我收到以下错误消息:
Traceback (most recent call last):
File "C:/Users/Owner/doHttpRequest.py", line 11, in <module>
response = connection.getresponse()
File "C:\Python27\lib\httplib.py", line 1015, in getresponse
raise ResponseNotReady()
ResponseNotReady
但是,我的服务器可以正常接收请求。我可以使用调试器检查服务器代码,并查看请求是否已正确传输以及所有标头。
为什么我的脚本说ResponseNotReady
?这不是一个大问题,因为我的脚本执行其主要任务(重放保存的 http 请求),但我希望能够使用脚本输出一些基本信息,例如状态代码,或者保存某处响应
HTTPConnection.send 用于发送 HTTP 请求的数据部分。因此,您应该只提供请求文件中的最后一行,而不是整个内容。
如果您想按原样使用您的请求,则应该使用套接字模块来代替与服务器的原始连接。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)