2011-04-18 9 views
0

私はポート6868で動作するローカルサーバーを持っています。技術的にはexpressで構築されたnode.js駆動のマイクロサイトです。実際には、いくつかのデータを読み込み、コンソールに書き込む単一の '/プッシュ'コンストーラーを持っています(質問に関係のないいくつかのオペラント)。python httplib timeout to localhost

カール

h100:~ eugenemirotin$ curl -i http://127.0.0.1:6868/push -d password=pwd 
HTTP/1.1 200 OK 
X-Powered-By: Express 
Connection: keep-alive 
Transfer-Encoding: chunked 

を用い、想定としてコンソールにwitesをNode.jsの。 Pythonとhttplibを使用して

h100:~ eugenemirotin$ python 
Python 2.7.1 (r271:86832, Jan 6 2011, 00:55:07) 
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import httplib, urllib 
>>> params = {'password': 'pwd', 'type': 'msg', 'channel': 'chat', 'client_id': '', 'body': {'text': 'test test'}} 
>>> params 
{'body': {'text': 'test test'}, 'password': 'pwd', 'type': 'msg', 'client_id': '', 'channel': 'chat'} 
>>> params = urllib.urlencode(params) 
>>> params 
'body=%7B%27text%27%3A+%27test+test%27%7D&password=pwd&type=msg&client_id=&channel=chat' 
>>> conn = httplib.HTTPConnection('http://127.0.0.1:6868') 
>>> conn.request("POST", "/push", params) 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request 
    self._send_request(method, url, body, headers) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request 
    self.endheaders(body) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders 
    self._send_output(message_body) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output 
    self.send(msg) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send 
    self.connect() 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 740, in connect 
    self.timeout, self.source_address) 
    File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection 
    raise err 
socket.error: [Errno 60] Operation timed out 
>>> quit() 

違いをパラメータには必須ではない - 要求でも、サーバーをNode.jsのために取得していません。

httplibのバグですか、何か問題がありますか?

答えて

4

アドレスからhttp://を削除します。

この:

conn = httplib.HTTPConnection('http://127.0.0.1:6868') 

は次のようになります。

conn = httplib.HTTPConnection('127.0.0.1:6868') 
+1

ああ、ありがとうございました。愚かなhttplib、私は – Guard

3

あなたはあなたのコードに多くを簡素化することができます。

import urllib, urllib2 
params = {'password': 'pwd', 'type': 'msg', 'channel': 'chat', 'client_id': '', 'body': {'text': 'test test'}}  
params = urllib.urlencode(params) 
res = urllib2.urlopen('http://127.0.0.1:6868/push/', params) 
data = res.read() 
res.close() 
+0

これはGET – Guard

+1

@ガードを実行すると思います:いいえ、それはPOSTを実行しません。ドキュメントを読んで: 'HTTPリクエストは、データパラメータが提供されたときにGETの代わりにPOSTになります' – vartec

+0

ああ、ありがとう、これは意味がある – Guard