【问题标题】:How should I handle this HTTPS request in Python?我应该如何在 Python 中处理这个 HTTPS 请求?
【发布时间】:2013-05-09 12:41:10
【问题描述】:

我正在尝试在 Python 中使用 Strava API v3,但恐怕我错过了一些东西。文档说:

此基本 URL 用于所有 Strava API 请求: https://api.strava.com

$ curl -i https://api.strava.com
HTTP/1.1 200 OK Content-Type: application/json Status: 200 OK
X-RateLimit-Limit: 5000 X-RateLimit-Remaining: 4999 Content-Length: 2

响应采用 JSON 格式并经过 gzip 压缩。

我目前正在这样做:

import urllib
print urllib.urlopen('https://api.strava.com').read()

然后开始:

Traceback (most recent call last):
  File "StravaAPIv3.py", line 3, in <module>
    print urllib.urlopen('https://api.strava.com').read()
  File "C:\Python27\lib\urllib.py", line 86, in urlopen
    return opener.open(url)
  File "C:\Python27\lib\urllib.py", line 207, in open
    return getattr(self, name)(url)
  File "C:\Python27\lib\urllib.py", line 436, in open_https
    h.endheaders(data)
  File "C:\Python27\lib\httplib.py", line 954, in endheaders
    self._send_output(message_body)
  File "C:\Python27\lib\httplib.py", line 814, in _send_output
    self.send(msg)
  File "C:\Python27\lib\httplib.py", line 776, in send
    self.connect()
  File "C:\Python27\lib\httplib.py", line 1157, in connect
    self.timeout, self.source_address)
  File "C:\Python27\lib\socket.py", line 553, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
IOError: [Errno socket error] [Errno 11004] getaddrinfo failed

我不知道从哪里开始,因为我对 HTTP 请求和 HTTPS 不太了解

更新:根据梅林的建议使用requests 模块,我正在这样做:

import requests

r = requests.get('https://api.strava.com/')
print r.status_code
print r.headers['content-type']
print r.encoding
print r.text
print r.json() 

但一直报错:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.strava.com', port=443): Max retries exceeded with url: / (Caused by <class 'so cket.gaierror'>: [Errno 11004] getaddrinfo failed)

【问题讨论】:

    标签: python https urllib getaddrinfo


    【解决方案1】:

    尝试使用请求!它更安全。 http://docs.python-requests.org/en/latest/

    【讨论】:

    • 收到此错误:requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.strava.com', port=443): Max retries exceeded with url: / (Caused by &lt;class 'so cket.gaierror'&gt;: [Errno 11004] getaddrinfo failed)
    • 我已使用生成此错误的代码编辑了问题。
    • 好吧,目前看来这东西太脆弱了(尽管他们打算在六月发布这个......)。 +1 为一个强大的 http 模块,然后! (我用 PIP 轻松安装)
    【解决方案2】:

    您需要先按照此处的说明进行操作:http://strava.github.io/api/v3/oauth/ 基本上,您创建的应用程序必须授权使用它的用户(在本例中为您)。我在下面写了一些示例代码。我是 python 新手,所以不知道如何自动登录,所以你必须将 url 复制并粘贴到浏览器并复制并粘贴代码。

    import requests
    import json
    
    #Replace #### with your client id (listed here: http://www.strava.com/settings/api)
    #Replace &&&& with your redirect uri listed on the same page. I used localhost
    #Go this url in your browser and log in. Click authorize
    https://www.strava.com/oauth/authorize?client_id=###&response_type=code&redirect_uri=&&&&
    
    #Copy and paste the code returned in the url
    code='qwertyuio123456789'
    #Replace @@@@ with the code on your api page
    values={'client_id':'###', 'client_secret':  '@@@@', 'code':code}
    r = requests.post('https://www.strava.com/oauth/token', data=values)
    json_string = r.text.replace("'", "\"")
    values = json.loads(json_string)
    
    #now you have an access token
    r = requests.get('http://www.strava.com/api/v3/athletes/227615', params=values)
    

    玩得开心!

    【讨论】:

    • 这看起来很有希望。我会尽快测试它并在这里给出一些反馈。谢谢!
    • 你能告诉我如何使用“localhost”来获取代码吗?我收到一个错误的请求错误...
    • 需要在strava api设置页面设​​置回调url。一旦用户获得授权,strava 会将用户重定向到该网站。由于您是在 python 中执行此操作,因此这无关紧要。因此,只需在 url 字符串和您帐户的设置页面中将回调 url 设置为 localhost。
    • 我明白了...问题是我应该首先要求注册我的应用程序。但目前 Strava 将注册仅限于少数开发者...嗯,非常感谢您的帮助,但我必须稍等一下,看来...
    【解决方案3】:

    您需要使用httplib。访问 HTTPS 服务器的示例代码:

    import httplib
    
    con = httplib.HTTPSConnection('www.google.com')
    con.request("GET", "/")
    res = con.getresponse()
    print res.read()
    

    【讨论】:

    • 收到此错误:httplib.InvalidURL: nonnumeric port: '//api.strava.com/'
    • 网址是否正确?当我在浏览器中尝试该 URL 并且 ping api.strava.com 返回“未知主机”时,我得到一个“网页”不可用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    相关资源
    最近更新 更多