python httplib 到本地主机的超时

发布于 2024-11-01 22:05:25 字数 2436 浏览 6 评论 0原文

我有一个在端口 6868 上运行的本地服务器。从技术上讲,它是使用 Express 构建的由 Node.js 驱动的微型站点。它实际上有一个“/push”控制器读取一些数据并写入控制台(+一些特定的与问题无关的操作)。

当使用curl

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

和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 错误,还是我做错了什么?

I have a local server running on port 6868. Technically it's node.js-driven micro-site built with express. It actually has a single '/push' constroller reading some data and writing to console (+ some specific not question-related operatons).

When using curl

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

and node.js wites to console as supposed.

When using python and 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()

The difference in parameters is not essential - the request doesn't even get to node.js server.

Is it httplib bug, or am I doing something wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瀞厅☆埖开 2024-11-08 22:05:25

从地址中删除 http://

这:

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

应该是:

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

Remove http:// from the address.

This:

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

Should be:

conn = httplib.HTTPConnection('127.0.0.1:6868')
拧巴小姐 2024-11-08 22:05:25

你可以大大简化你的代码:

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

You can simplify your code a lot:

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文