问题描述
我正在python中编写一个简单的脚本来。
I'm writing a simple script in python to replay saved HTTP requests.
以下是脚本:
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
当我运行此脚本时,收到以下错误消息:
When I run this script, I get this error message:
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
但是,我的服务器收到请求。我可以使用调试器检查服务器代码,并查看请求是否已正确传输,包含所有标头。
However, my server receives the request fine. I can examine the server code with the debugger and see that the request has been transmitted correctly, with all headers.
为什么我的脚本说 ResponseNotReady
?这不是一个大问题,因为我的脚本执行其主要任务(重放保存的http请求)但我希望能够输出一些基本信息与脚本,如状态代码,或保存的内容响应某处
Why is my script saying ResponseNotReady
? This isn't a huge issue as my script performs its main task (to replay saved http requests) but I'd like to be able to output some basic information with the script, such as status codes, or to save the contents of the response somewhere
推荐答案
HTTPConnection.send用于发送HTTP请求的数据部分。所以,你应该只提供你的请求文件中的最后一行而不是整个行。
HTTPConnection.send is for sending the data portion of an HTTP request. So, you should only be supplying the last line in your request file rather than the whole thing.
如果你想按原样使用你的请求,你应该使用套接字模块,而不是与服务器的原始连接。
If you want to use your request as is, you should be using the socket module instead for a raw connection to the server.
这篇关于ResponseNotReady真的很简单的python http请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!