ResponseNotReady真的很简单的python http请求?

时间:2011-12-05 12:27:12

标签: python http

我在python中将一个简单的脚本写到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

当我运行此脚本时,收到此错误消息:

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请求)时,这不是一个巨大的问题,但我希望能够使用脚本输出一些基本信息,例如状态代码,或者保存某处的回应内容

2 个答案:

答案 0 :(得分:1)

HTTPConnection.send用于发送HTTP请求的数据部分。因此,您应该只提供请求文件中的最后一行而不是整个行。

如果要按原样使用请求,则应使用套接字模块代替与服务器的原始连接。

答案 1 :(得分:1)

我如何使用“socket”模块的例子,可能对某人有用

input_request_string = '''GET /cherry/mirror_simple HTTP/1.1\r\nContent-Length: 0\r\nHost: test-host.dom:8001\r\n\r\n'''

host = 'test-host.dom'
port = 8001

# Connect to the server
s = socket.socket()
s.connect((host, port))
s.send(request_string)

from httplib import HTTPResponse    
response = HTTPResponse(s)
response.begin()
body = response.read()
headers = str(response.msg)
s.close()