当前位置: 首页 > 知识库问答 >
问题:

Python请求基本命令返回错误-怎么了?

窦夜洛
2023-03-14

我想运行以下3个命令:

import requests
result = requests.get("https://somedomain.xyz/start_page.html")
print(result.status_code)

但我得到一个错误输出:

Traceback (most recent call last):
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 670, in urlopen
    httplib_response = self._make_request(
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 381, in _make_request
    self._validate_conn(conn)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 978, in _validate_conn
    conn.connect()
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connection.py", line 362, in connect
    self.sock = ssl_wrap_socket(
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\ssl_.py", line 386, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1040, in _create
    self.do_handshake()
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\ssl.py", line 1309, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 439, in send
    resp = conn.urlopen(
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\connectionpool.py", line 726, in urlopen
    retries = retries.increment(
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\urllib3\util\retry.py", line 446, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='somedomain.xyz', port=443): Max retries exceeded with url: /bzo/en/start_page.html (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Python\TTNr.py", line 8, in 
    result = requests.get("https://somedomain.xyz/start_page.html")
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 76, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 530, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\sessions.py", line 643, in send
    r = adapter.send(request, **kwargs)
  File "C:\Users\usik\AppData\Local\Programs\Python\Python38-32\lib\site-packages\requests\adapters.py", line 514, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='somedomain.xyz', port=443): Max retries exceeded with url: /start_page.html (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1108)')))

有人知道什么是问题以及如何解决吗?提前谢谢。

共有2个答案

樊浩初
2023-03-14

您似乎正在访问一个使用自签名证书的站点。默认情况下,这将被视为无效。

看看:如何让Python请求信任自签名SSL证书?

席言
2023-03-14
import requests
result = requests.get("https://somedomain.xyz/start_page.html", verify=False)
print(result.status_code)

如果您不想保留verify=False,则需要在此处提及相关证书的路径。

 类似资料:
  • 问题内容: 这工作正常: 这将返回400 Bad Request(只是使用.ajax来支持错误处理的上述jQuery的重新格式)。 问题答案: 我认为您只需要再添加2个选项(和):

  • 我试图使用Gmail API将用户设置应用到Gmail帐户,但它不断返回错误400错误请求。 我可以看到错误代码在Gmail API控制台,它来自我的服务号,所以代码不可能是如此错误,但它让我发疯,只是不能找出什么是错误的。 如果有人能给我指出正确的方向,我会非常感激。

  • 这是我的控制器,它将请求映射到以下url

  • 我试图对我写的rest api执行一个put请求,但它总是返回400-bug请求。 java.io.IO异常:服务器返回HTTP响应代码:400为URL:http://localhost:8000/api/v0/contacts/2sun.net.www.protocol.http.HttpURLConnection.getInputStream(未知来源) 我可以用firefox rest客户端

  • 问题内容: 我有这个应用程序,它可以在本地运行,并且在部署时可以使用.mdf SQL Express数据库文件(通常用于测试目的)。但是,当我将其更改为可与我们的SQL Server 2008一起使用时,该应用程序可以运行,但该服务无法运行。 例如,如果在页面后面的代码中,我有一个按钮可以向表中添加数据,例如: 我的web.config设置为在该服务器上使用模拟,并且一切运行良好。但是,对于我的服

  • 对localhost的Ajax请求返回403错误。然而,当我更改controller requestMethod以获取并在浏览器上打开请求“url”时,它会显示从服务器返回的json数据。我希望使用这些数据填充一个下拉列表。请帮帮我。我正在使用spring security 4.0和spring MVC框架。CSRF未禁用。此外,该url在Spring得到保护。 我在这里查看了相关问题,但没有找到