首页 > 解决方案 > 使用 Python 请求建立新连接失败错误 Errno -2 Name or service unknown

问题描述

我正在尝试使用 Python 向 API 发出请求。我可以毫无问题地使用 curl 发出请求,但我的 Python 请求有问题。

为什么这段代码,

import requests
from requests.auth import HTTPBasicAuth

emailadd = 'user123@example.com'
domain = 'example.com'

spantoken = 'omitted'

def checkUserLicensed(useremail):
     url = ('https://api.spannigbackup.com/v1/users/' + useremail)
     print(url)
     response = requests.get(url, auth=(domain,spantoken))
     print(response)

checkUserLicensed(emailadd)

返回此错误

requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.spannigbackup.com', port=443): Max retries exceeded with url: /v1/users/user123@example.com 
(Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f73ca323748>: Failed to establish a new connection: [Errno -2] Name or service not known'))

标签: pythonapipython-requests

解决方案


推荐阅读