【问题标题】:SSL Certificate Error while making a POST request to an external server向外部服务器发出 POST 请求时出现 SSL 证书错误
【发布时间】:2020-03-18 05:23:26
【问题描述】:

我正在尝试使用 requests 库向 https://app.getswift.co/api/public/v2/deliveries 发出发布请求,但出现以下错误

HTTPSConnectionPool(host='app.getswift.co', port=443): Max retries exceeded with url: /api/public/v2/deliveries (Caused by SSLError(CertificateError("hostname 'app.getswift.co' doesn't match either of '*.my_domain.ai', 'another.my_domain.ai'),))

这是我提出的要求。

headers = {
    'Content-Type': 'application/json',
}

data = {
    "apiKey": "my-api-key",
    "booking": {
        "pickupDetail": {
            "name": "Rupert 1",
            "phone": "1234567890",
            "address": "112 luscombe st, brunswick, melbourne"
        },
        "dropoffDetail": {
            "name": "Igor 2",
            "phone": "0987654321",
            "address": "105 collins st, 3000"
        }
    }
}
requests.post("https://app.getswift.co/api/public/v2/deliveries", headers=headers, json=data)

我在本地 python shell 上测试了相同的代码,甚至发出了邮递员请求,它工作得非常好。当我尝试将相同的代码推送到 ec2 服务器并进行发布请求调用时,问题就出现了。

我尝试使用verify=False 来查看它是否有效(即使我知道安全后果),但我却得到了"404 Not found"。 这是我在 ec2 日志中看到的内容

Not Found: /api/public/v2/deliveries 

HTTP POST /api/public/v2/deliveries 404 [0.01, 127.0.0.1:57840] 
https://app.getswift.co:443 "POST /api/public/v2/deliveries HTTP/1.1" 404 None

我将不胜感激。

【问题讨论】:

    标签: python-3.x ssl post python-requests ssl-certificate


    【解决方案1】:

    如果不访问确切的页面很难判断,但您能否添加其他标题,例如 User-Agent 和 cookies=cookies?我认为该页面可能正在检查身份验证,而您没有正确进行身份验证。

    否则,ec2 服务器也可能像 AWS 服务器一样位于代理/vpn 后面。在这种情况下,您可以尝试信任环境。

    s = requests.Session()
    s.trust_env = False
    
    response = s.get("https://app.getswift.co/api/public/v2/deliveries", headers=headers, json=data)
    

    如果它不起作用,那么 API 可能只是阻塞了您的 ec2 服务器。尝试在另一个区域制作一个 ec2 或使用另一个 VPS,看看它是否有效。

    【讨论】:

    • "verfiry=False" 给出问题中提到的“404 not found”
    • 它是如何从我的本地、邮递员和没有其他标头的 python shell 中工作的?
    • 确实如此。无法访问网页很难猜测,所以我添加了另一个猜测,不确定它是否适合您。
    • 我用 trust_env False 尝试了你的解决方案,但它给出了相同的 SSLError。实际上没有网页,当我收到订单时,我会发出这个请求,将订单详细信息从后端发送到服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-07
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多