【问题标题】:I am trying to retrieve a URL but keep running into an SSLError: HTTPSConnectionPool(host='developer.uspto.gov', port=443)我正在尝试检索 URL,但一直遇到 SSLError: HTTPSConnectionPool(host='developer.uspto.gov', port=443)
【发布时间】:2019-07-26 15:04:11
【问题描述】:

我正在尝试从 USPTO 设置的 API 中检索 URL。他们的系统为查询提供了一个 URL,在 Web 浏览器中搜索时它工作得很好。但是在 Python3 中这样做时我一直收到此错误

我已经尝试使用 urllib 和 requests 来检索数据。

我的代码:

import requests

link = new_url
f = requests.get("https://developer.uspto.gov/ibd-api/v1/patent/application?searchText=device&start=0&rows=2000")
print(f.text)

错误:

SSLError: HTTPSConnectionPool(host='developer.uspto.gov', port=443): Max 
retries exceeded with url: /ibd-api/v1/patent/application? 
searchText=device&start=0&rows=2000 (Caused by SSLError(SSLError("bad 
handshake: Error([('SSL routines', 'tls_process_server_certificate', 
'certificate verify failed')])")))

我希望能够使用 json 库读取此 URL 的内容。

【问题讨论】:

    标签: python python-3.x python-requests urllib


    【解决方案1】:

    通过在您的获取请求中添加verify = False 可以轻松解决此错误。

    我建议用这个替换你当前的代码:

    import requests
    
    link = new_url
    f = requests.get("https://developer.uspto.gov/ibd-api/v1/patent/application?searchText=device&start=0&rows=2000", verify=False)
    print(f.text)
    

    Here 是 SSL 证书验证的更多信息。

    希望对你有帮助

    【讨论】:

    • 这实际上与verify=False 相同。它会起作用,但它不是理想的方法,并且会使请求容易受到 MITM 攻击。
    • @b_c 这是一个公平的观点,我现在将修改我的答案(仅将 ssl.CERT_NONE 替换为 False)。但据我了解,OP 只是想从某个来源获取信息,他并没有尝试构建 API。
    【解决方案2】:

    您的网址似乎遇到了与this question 相同的问题(accepted answer 描述了该问题;Here's the SSLLabs report on the host

    通过从您的原始 URL 下载证书,将两个 Entrust 证书导出到它们自己的文件(Entrust 证书颁发机构 - L1K 和 Entrust.net),然后创建 .pem 信任链,我能够解决 SSL 问题其中(响应中缺少 Entrust L1K 证书):

    cat entrustL1K.cer entrustNET.cer > entrust_chain.pem
    

    然后,您可以将此信任链传递给requests.get 以修复响应:

    url = "https://developer.uspto.gov/ibd-api/v1/patent/application?searchText=device&start=0&rows=2000"
    requests.get(url, verify='entrust_chain.pem')
    
    >>> <Response [200]>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-31
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2021-01-01
      相关资源
      最近更新 更多