recognize_google报WinError10054:VPN适配、原因及替代方案咨询
语音转文字recognize_google函数VPN环境下故障排查与解决方案
问题场景
运行网上找到的语音转文字示例时,仅在开启VPN连接时,recognize_google函数执行失败,报错如下:
the surrounding noise level listens for the user's input Using google to recognize audio Traceback (most recent call last): File "C:\Users\pvpkumar\PycharmProjects\AI_Jedai\aud_inp_reco.py", line 41, in <module> MyText = r.recognize_google(audio2) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\site-packages\speech_recognition\_init_.py", line 840, in recognize_google response = urlopen(request, timeout=self.operation_timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 216, in urlopen return opener.open(url, data, timeout) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 519, in open response = self._open(req, data) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\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\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 496, in _call_chain result = func(*args) ^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 1377, in http_open return self.do_open(http.client.HTTPConnection, req) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\urllib\request.py", line 1352, in do_open r = h.getresponse() ^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 1374, in getresponse response.begin() File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 318, in begin version, status, reason = self._read_status() ^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\http\client.py", line 279, in _read_status line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\pvpkumar\AppData\Local\Programs\Python\Python311\Lib\socket.py", line 706, in readinto return self._sock.recv_into(b) ^^^^^^^^^^^^^^^^^^^^^^^ ConnectionResetError: [WinError 10054] An existing connection was forcibly closed by the remote host Process finished with exit code 1
对应的运行代码:
import speech_recognition as sr import pyttsx3 # Initialize the recognizer r = sr.Recognizer() # Function to convert text to # speech def SpeakText(command): # Initialize the engine engine = pyttsx3.init() engine.say(command) engine.runAndWait() # Loop infinitely for user to # speak while (1): # Exception handling to handle # exceptions at the runtime try: # use the microphone as source for input. with sr.Microphone() as source2: # wait for a second to let the recognizer # adjust the energy threshold based on # the surrounding noise level print("the surrounding noise level") r.adjust_for_ambient_noise(source2, duration=0.2) # listens for the user's input print("listens for the user's input") audio2 = r.listen(source2) # Using google to recognize audio print("Using google to recognize audio") MyText = r.recognize_google(audio2) MyText = MyText.lower() print("Did you say ", MyText) SpeakText(MyText) except sr.RequestError as e: print("Could not request results; {0}".format(e)) except sr.UnknownValueError: print("unknown error occurred")
1. 故障原因
- VPN节点IP被Google拦截:部分VPN共享节点IP因频繁请求、违反使用策略,被Google的反访问限制机制判定为异常,强行关闭连接。
- VPN链路不稳定:VPN节点与Google服务器间的网络出现丢包、延迟过高,导致TCP传输音频数据时被远程端强制终止连接。
- VPN流量过滤冲突:部分VPN会对HTTPS请求做中间人拦截或修改请求头,导致Google服务器无法识别合法请求,主动断开连接。
2. recognize_google的可用替代方案
- recognize_google_cloud:使用Google Cloud官方语音识别API,需申请API密钥,稳定性和准确率更高,支持多语言与自定义模型,替换代码调用方法并传入密钥即可。
- recognize_wit:调用Wit.ai语音识别服务,需注册获取API密钥,支持多语言,适合轻量级应用。
- recognize_sphinx:CMU Sphinx本地识别引擎,无需联网,适合离线场景,但准确率低于在线服务,中文支持有限。
- recognize_azure:微软Azure语音服务,提供免费额度,支持多语言与实时识别,需申请Azure账号和密钥。
3. VPN设置调整建议
- 切换VPN节点:优先选择正规商用的欧美地区节点,避免使用免费/共享IP的节点,降低被Google拦截的概率。
- 启用流量白名单:将
speech.googleapis.com、www.google.com等Google语音识别相关域名加入VPN白名单,让这类流量不经过VPN,直接走本地网络。 - 关闭HTTPS拦截:若VPN开启了SSL/TLS中间人拦截功能,需关闭该选项,避免Google服务器验证请求失败。
- 切换传输协议:尝试使用WireGuard、OpenVPN UDP等更稳定的协议,减少网络波动引发的连接重置问题。
内容的提问来源于stack exchange,提问作者Pvp Kumar
相关产品推荐
相关产品推荐

