如何在客户端使用带有 Python 请求的证书和密钥文件?

时间:2021-04-20 10:20:44

标签: python https python-requests

我有一个家庭作业,我必须与服务器通信。我正在完成最后一个任务,但我无法让它工作。任务如下:

  • 连接到 http://[服务器 IP] 并下载 cert.php 和 key.php 文件。
  • 之后,使用这些文件获取 https://[服务器 IP]。
  • 如果 HTTPS 请求成功,服务器将回复唯一标志。 (硬件的解决方案)

我可以下载文件没问题,但是当我尝试使用它们时,出现错误:

dir1 = filedialog.askopenfile(mode="rb", initialdir="/", title="select file",
                                       filetypes=(("text files", "*.txt"), ("all files","*.*")))

我也试过用 Traceback (most recent call last): File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 699, in urlopen httplib_response = self._make_request( File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 382, in _make_request self._validate_conn(conn) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 1010, in _validate_conn conn.connect() File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connection.py", line 411, in connect self.sock = ssl_wrap_socket( File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 432, in ssl_wrap_socket ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\ssl_.py", line 474, in _ssl_wrap_socket_impl return ssl_context.wrap_socket(sock) File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 500, in wrap_socket return self.sslsocket_class._create( File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1040, in _create self.do_handshake() File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\ssl.py", line 1309, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 439, in send resp = conn.urlopen( File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\connectionpool.py", line 755, in urlopen retries = retries.increment( File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\urllib3\util\retry.py", line 574, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='152.66.249.144', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122)'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\user\PycharmProjects\pythonProject\HF\hf_script.py", line 195, in <module> final_conn = requests.get(url_https, File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 76, in get return request('get', url, params=params, **kwargs) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\api.py", line 61, in request return session.request(method=method, url=url, **kwargs) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 542, in request resp = self.send(prep, **send_kwargs) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\sessions.py", line 655, in send r = adapter.send(request, **kwargs) File "C:\Users\user\PycharmProjects\pythonProject\venv\lib\site-packages\requests\adapters.py", line 514, in send raise SSLError(e, request=request) requests.exceptions.SSLError: HTTPSConnectionPool(host='152.66.249.144', port=443): Max retries exceeded with url: / (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate (_ssl.c:1122)'))) Process finished with exit code 1 扩展名保存文件,我什至不知道这是否有意义,但没有用。此外,我尝试使用 .pem 并创建一个未经验证的上下文,但它显示 ssl

这是我使用的代码:

Cannot find reference '_create_unverified_context' in 'ssl.pyi'

1 个答案:

答案 0 :(得分:0)

好的,我知道了。我怀疑有人会使用它,但我使用的证书和密钥很好,问题是,服务器本身使用的是自签名证书,无论如何我都必须建立连接。对我有用的解决方案:

final_conn = requests.get(url_https,
                          cert=('C:/Users/user/PycharmProjects/pythonProject/HF/cert.php',
                                'C:/Users/user/PycharmProjects/pythonProject/HF/key.php'),
                          verify=False)