使用SSL证书的Django请求

问题描述 投票:0回答:1

在一个django视图中,我需要向另一个API发起一个post请求,该API要求在请求中附加SSL证书。我有ssl.crt文件。

我如何实现这个功能?

我试着在这里使用这个实现。https:/gist.github.comerikbern756b1d8df2d1487497d29b90e81f8068。

但是,即使我以管理员身份运行pycharm,也会得到这个错误。

Traceback (most recent call last):
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Applications\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "", line 350, in pge_auth_v1_redirect_view
    with pfx_to_pem(pem_file_path, '') as cert:
  File "c:\program files\python37\Lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "", line 271, in pfx_to_pem
    f_pem = open(t_pem.name, 'wb')
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\fpier\\AppData\\Local\\Temp\\tmpg7i7qtun.pem'
python python-requests pyopenssl
1个回答
0
投票

我找到的解决方案是将公有文件和私有文件合并创建一个pem文件。

        req = requests.post(
            url,
            params=params,
            headers=headers,
            auth=client_auth,
            verify=True,
            cert=self._pem_file_path
        )
© www.soinside.com 2019 - 2024. All rights reserved.