在/ home [Errno -2]的gai错误名称或服务未知

时间:2011-12-28 21:55:12

标签: python django https django-views httplib

根据httplib docs中的示例:

>>> import httplib, urllib
>>> params = urllib.urlencode({'@number': 12524, '@type': 'issue', '@action': 'show'})
>>> headers = {"Content-type": "application/x-www-form-urlencoded",
...            "Accept": "text/plain"}
>>> conn = httplib.HTTPConnection("bugs.python.org")
>>> conn.request("POST", "", params, headers)
>>> response = conn.getresponse()
>>> print response.status, response.reason
302 Found
>>> data = response.read()
>>> data
'Redirecting to <a href="http://bugs.python.org/issue12524">http://bugs.python.org/issue12524</a>'
>>> conn.close()

我的代码是:

import httplib
import urllib

token = request.POST.get('token')
if token:
    params = urllib.urlencode({'apiKey':'[some string]', 'token':token})
    connection = httplib.HTTPSConnection('rpxnow.com/api/v2/auth_info')
    connection.request('POST', "", params)
    response = connection.getresponse()
    print response.read()
检查我当地的vars yeilds:

连接:“httplib.HTTPSConnection实例位于0x8baa4ac” params:'token = [some string]&amp; apiKey = [some string]'

(我打电话的说明是:

使用令牌进行auth_info API调用: 网址:https://rpxnow.com/api/v2/auth_info 参数:

apiKey     [一些字符串] 代币     您在上面提取的令牌值

但是我收到主题行中提到的错误。为什么呢?

3 个答案:

答案 0 :(得分:4)

你误解了httplib的文档。实例化HTTPSConnection的参数只是主机名。然后,将实际路径作为第二个参数传递给request。所以:

connection = httplib.HTTPSConnection('rpxnow.com')
connection.request('POST', '/api/v2/auth_info', params)

答案 1 :(得分:0)

我不知道rpxnow.com是什么,我不熟悉他们的API,但是这条错误消息表明他们没有响应该URL请求的服务(即'rpxnow.com/api/v2 / auth_info')。

您是否能够验证其服务是否已启动并在该网址上运行?

答案 2 :(得分:0)

尝试使用:

http://docs.python-requests.org/en/latest/user/quickstart/#make-a-post-request

import requests

payload = {'apiKey':'somevalue', 'token':'some_token'}
r = requests.post('https://rpxnow.com/api/v2/auth_info', data=payload)
r.content