request报错:SSLError: [SSL: WRONG_VERSION_NUMBER]


ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1007)

问题现象

语音合成功能突然不能用了,查看后台log发现TTS_BertVits2API的请求一直失败,报错为ssl握手失败。

(vstore) G:\oddmeta\xiaoluo\back-end\omserver\omengine>python tts\tts_bert_vits2.py
params={'data': ['晚上好', '流萤_ZH', 0.5, 0.6, 0.9, 1, 'ZH', False, 1, 0.2, None, 'Happy', '', 0.7], 'event_data': None, 'fn_index': 0, 'session_hash': '1c660151-694e-4838-a0cd-fc37304a76a6'}
params={'data': ['晚上好', '流萤_ZH', '0.5', '0.6', '0.9', '1', 'ZH', 'False', '1', '0.2', None, 'Happy', '', '0.7'], 'event_data': None, 'fn_index': '0', 'session_hash': '1c660151-694e-4838-a0cd-fc37304a76a6'}
params2={'data': ['晚上好', '流萤_ZH', '0.5', '0.6', '0.9', '1', 'ZH', 'False', '1', '0.2', None, 'Happy', '', '0.7'], 'event_data': None, 'fn_index': '0', 'session_hash': '1c660151-694e-4838-a0cd-fc37304a76a6'}
Traceback (most recent call last):
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connectionpool.py", line 597, in urlopen
    httplib_response = self._make_request(conn, method, url,
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connection.py", line 337, in connect
    self.sock = ssl_wrap_socket(
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\util\ssl_.py", line 345, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "D:\anaconda3\envs\vstore\lib\ssl.py", line 513, in wrap_socket
    return self.sslsocket_class._create(
  File "D:\anaconda3\envs\vstore\lib\ssl.py", line 1071, in _create
    self.do_handshake()
  File "D:\anaconda3\envs\vstore\lib\ssl.py", line 1342, in do_handshake
    self._sslobj.do_handshake()
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1007)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connectionpool.py", line 637, in urlopen
    retries = retries.increment(method, url, error=e, _pool=self,
  File "D:\anaconda3\envs\vstore\lib\site-packages\urllib3\util\retry.py", line 399, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='v2.genshinvoice.top', port=443): Max retries exceeded with url: /run/predict (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1007)')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "G:\oddmeta\xiaoluo\back-end\omserver\omengine\tts\tts_bert_vits2.py", line 688, in <module>
    client.do_synthesis(text="晚上好", speaker="流萤_ZH", noise=0.6, noisew=0.9, sdp_ratio=0.5)
  File "G:\oddmeta\xiaoluo\back-end\omserver\omengine\tts\tts_bert_vits2.py", line 666, in do_synthesis
    return self.client.request(req_params=params_str_values)
  File "G:\oddmeta\xiaoluo\back-end\omserver\omengine\tts\tts_bert_vits2.py", line 619, in request
    response = requests.post(url, headers=headers, data=body, verify=False)
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\api.py", line 115, in post
    return request("post", url, data=data, json=json, **kwargs)
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
  File "D:\anaconda3\envs\vstore\lib\site-packages\requests\adapters.py", line 517, in send
    raise SSLError(e, request=request)
requests.exceptions.SSLError: HTTPSConnectionPool(host='v2.genshinvoice.top', port=443): Max retries exceeded with url: /run/predict (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1007)')))

我使用的是免费的原神角色语音合成,打开其网页地址:https://v2.genshinvoice.top ,发现在浏览器中就会报不安全证书的警告。

问题排查

加verify=False跳过证书校验:失败

考虑到目标网址的证书异常,我在请求中给加了个verify=False,再跑,错误依旧。

response = requests.post(url, headers=headers, data=body, verify=False)

查看当前环境

(vstore) G:\oddmeta\xiaoluo\back-end\omserver\omengine>pip list | findstr "urllib"
urllib3                  1.24

(vstore) G:\oddmeta\xiaoluo\back-end\omserver\omengine>python --version
Python 3.10.12

降级urllib3到1.23: 搞定

pip install urllib3==1.23

问题解决。

屏蔽告警

由于https://v2.genshinvoice.top 的证书有问题,即使在浏览器上访问也会告警,用urllib3去请求同样会出现一个告警。

D:\anaconda3\envs\vstore\lib\site-packages\urllib3\connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn((

如果你觉得这个告警烦人,可以用这个方式屏蔽掉。

import urllib3
urllib3.disable_warnings()

Leave a comment

Your email address will not be published. Required fields are marked *