Linux上使用python httplib2的问题

0 投票
1 回答
2994 浏览
提问于 2025-04-16 20:55

我在用下面这段代码来调用一个需要基本认证的网络服务:

import urllib
import httplib2
url='http://heywatch.com/download.json'
body={'username':'my_username','password':'my_password'}
header_data={'Accept': 'application/json'}
http=httplib2.Http()
response, content = http.request(url, 'POST', headers=header_data,     body=urllib.urlencode(body))

我在两个不同的环境中运行这段代码:

一个是Mac OSX 10.6.6,python版本是2.6.1 - 这边能得到正确的响应

另一个是Linux - centos,python版本是2.6.6 - 但我遇到了以下异常:

.....
File "/usr/lib/python2.6/site-packages/httplib2/__init__.py", line 1129, in _conn_request
    raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
ServerNotFoundError: Unable to find the server at heywatch.com

我在多个不同的Linux系统上用不同的python版本运行这段代码,结果都很好。我还发现curl在那台特定的机器上也不太正常。下面这个curl命令(和上面的代码效果一样)在我的Mac OSX上运行得很好:

curl -u username:password -X POST -d "url=https://s3.amazonaws.com/my_video_to_be_encoded.mov" -d "title=new_video" -d "automatic_encode=true" -d "ping_url_after_encode=http://mydomain/video_encode_success" https://heywatch.com/download.json

但是在出问题的Linux工作站上却失败了,显示了以下信息:

curl: (6) Couldn't resolve host 'heywatch.com'

有人知道为什么会这样吗?感觉像是某个操作系统的设置不对。如果有帮助的话,我是在亚马逊EC2上使用一个亚马逊版本的Linux,这个系统相对比较封闭。

1 个回答

0

这跟你的Linux电脑是怎么找到heywatch.com这个网站有关。你可以试着用ping命令检查一下heywatch.com能不能访问(可能访问不了)。你还可以查看一下你Linux电脑上的/etc/resolv.conf文件,看看里面配置了哪些域名服务器,并问问亚马逊你是不是在用他们的服务器。

顺便说一下,我刚在我的Linode上的Ubuntu系统上试了一下,发现heywatch.com可以正常访问。

撰写回答