gpt4 book ai didi

python - 我应该如何在 Python 中处理这个 HTTPS 请求?

转载 作者:行者123 更新时间:2023-11-28 21:24:04 25 4
gpt4 key购买 nike

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

This base URL is used for all Strava API requests: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

Responses are in JSON format and gzipped.

我目前正在这样做:

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 了解不多

更新:根据 Merlin 关于使用 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)

最佳答案

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

关于python - 我应该如何在 Python 中处理这个 HTTPS 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16462010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com