2011-12-05 8 views
1

私はPythonの単純なスクリプトをreplay saved HTTP requestsに書いています。ここで本当にシンプルなpython httpリクエストのResponseNotReady?

はスクリプトです:

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要求を再生するために)メインタスクを実行するので大きな問題ではありませんが、スクリプトを使ってステータスコードなどの基本情報を出力したり、 response somewhere

答えて

1

HTTPConnection.sendは、HTTPリクエストのデータ部分を送信するためのものです。だから、あなたはリクエストファイルの最後の行だけを提供するべきではありません。

リクエストをそのまま使用する場合は、サーバーへの未接続の代わりにソケットモジュールを使用する必要があります。私は「ソケット」モジュールを使用する方法の

+0

痛いのために役に立つかもしれない、それは(ソケットが使用することを困難に聞こえる)の痛みのようなものです。 'HttpConnection'の送信部分がうまくいけば、受け取り部分だけが壊れてしまい、変です。 – Oliver

1

例は、誰か

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() 
関連する問題