ResponseNotReady for really simple python http req

2019-03-02 12:57发布

I'm writing a simple script in python to replay saved HTTP requests.

Here is the script:

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()

And here is 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.

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

标签: python http
2条回答
太酷不给撩
2楼-- · 2019-03-02 13:22

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.

查看更多
▲ chillily
3楼-- · 2019-03-02 13:24

example of how I use "socket" module, might be useful for someone

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()
查看更多
登录 后发表回答