Pyngrok下载ngrok时SSL证书验证失败问题求助
问题描述
我要给FastAPI服务器搭建隧道,通过Poetry安装了pyngrok,也创建并激活了虚拟环境。pyproject.toml配置如下:
[tool.poetry.dependencies] python = "^3.11" fastapi = "^0.100.1" uvicorn = "^0.23.2" pyngrok = "^6.0.0" httpx = "^0.24.1"
在虚拟环境中运行ngrok http 9000时,出现SSL证书过期的报错,完整错误日志如下:
Traceback (most recent call last): File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 1348, in do_open h.request(req.get_method(), req.selector, req.data, headers, File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1282, in request self._send_request(method, url, body, headers, encode_chunked) File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1328, in _send_request self.endheaders(body, encode_chunked=encode_chunked) File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1277, in endheaders self._send_output(message_body, encode_chunked=encode_chunked) File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1037, in _send_output self.send(msg) File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 975, in send self.connect() File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1454, in connect self.sock = self._context.wrap_socket(self.sock, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 517, in wrap_socket return self.sslsocket_class._create( ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1075, in _create self.do_handshake() File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\ssl.py", line 1346, in do_handshake self._sslobj.do_handshake() ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\installer.py", line 117, in install_ngrok download_path = _download_file(url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\installer.py", line 261, in _download_file response = urlopen(url, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 216, in urlopen return opener.open(url, data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 519, in open response = self._open(req, data) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 536, in _open result = self._call_chain(self.handle_open, protocol, protocol + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 496, in _call_chain result = func(*args) ^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 1391, in https_open return self.do_open(http.client.HTTPSConnection, req, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\sergey\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 1351, in do_open raise URLError(err) urllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)> During handling of the above exception, another exception occurred: Traceback (most recent call last): File "<frozen runpy>", line 198, in _run_module_as_main File "<frozen runpy>", line 88, in _run_code File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Scripts\ngrok.exe\__main__.py", line 7, in <module> File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\ngrok.py", line 527, in main run(sys.argv[1:]) File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\ngrok.py", line 513, in run install_ngrok(pyngrok_config) File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\ngrok.py", line 100, in install_ngrok installer.install_ngrok(pyngrok_config.ngrok_path, pyngrok_config.ngrok_version) File "C:\Users\sergey\AppData\Local\pypoetry\Cache\virtualenvs\fast-api-telegram-bot-a-NRO46l-py3.11\Lib\site-packages\pyngrok\installer.py", line 121, in install_ngrok raise PyngrokNgrokInstallError("An error occurred while downloading ngrok from {}: {}".format(url, e)) pyngrok.exception.PyngrokNgrokInstallError: An error occurred while downloading ngrok from https://bin.equinox.io/c/bNyj1mQVY4c/ngrok-v3-stable-windows-amd64.zip: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired (_ssl.c:992)>
我已通过浏览器正常下载该压缩包,也能运行ngrok.exe,但问题仍未解决,求可行的解决办法。
解决办法
1. 手动指定已下载的ngrok路径
既然已经手动获取了ngrok,直接让pyngrok使用本地文件即可:
- 找到解压后的
ngrok.exe路径,比如C:\Users\sergey\Downloads\ngrok-v3-stable-windows-amd64\ngrok.exe - 临时指定路径运行命令:
pyngrok --ngrok-path "C:\Users\sergey\Downloads\ngrok-v3-stable-windows-amd64\ngrok.exe" http 9000 - 或者设置环境变量永久生效:
在Windows命令行执行:
设置完成后重启终端,之后直接运行setx NGROK_PATH "C:\Users\sergey\Downloads\ngrok-v3-stable-windows-amd64\ngrok.exe"ngrok http 9000即可。
2. 临时绕过SSL证书验证(仅测试用,不推荐生产环境)
如果是Python的SSL证书校验导致下载失败,可临时关闭验证:
- 命令行临时禁用:
set PYNGROK_SSL_VERIFY=false && ngrok http 9000 - 代码中设置:
from pyngrok import conf, ngrok conf.get_default().ssl_verify = False ngrok.connect(9000)
3. 更新系统根证书
SSL证书过期问题可能源于系统根证书未更新:
- Windows系统:打开
设置->更新和安全->Windows更新,检查并安装所有可用更新,尤其是证书相关的更新。 - 更新完成后重启电脑,再尝试运行命令。
4. 升级pyngrok版本
旧版本pyngrok可能使用的下载链接存在证书问题,升级到最新版试试:
poetry update pyngrok
内容的提问来源于stack exchange,提问作者WideWood
相关产品推荐
相关产品推荐

