为什么调用时python版本的演示api的翻译api超时

时间:2018-12-06 09:00:44

标签: python api timeout translation

# Imports the Google Cloud client library
from google.cloud import translate

# Instantiates a client
translate_client = translate.Client()

# The text to translate
text = u'Hello, world!'

# The target language
target = 'ru'

# Translates some text into Russian
translation = translate_client.translate(
    text,
    target_language=target)
print(u'Text: {}'.format(text))
print(u'Translation: {}'.format(translation['translatedText']))

我使用python版本演示的Google翻译api,发现方法translate_client.translate始终抛出超时异常,这是怎么回事?timeout when the method called.

回溯(最近通话最近):   文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”,行600,在urlopen中     分块=块状)   _make_request中的第343行的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”     self._validate_conn(conn)   _validate_conn中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”,行839     conn.connect()   连接中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connection.py”,第344行     ssl_context =上下文)   ssl_wrap_socket中的第344行的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ util \ ssl_.py”     返回context.wrap_socket(sock,server_hostname = server_hostname)   wrap_socket中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行412     会话=会话   _create中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行850     self.do_handshake()   在do_handshake中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行1108     self._sslobj.do_handshake() OSError:[Errno 0]错误

在处理上述异常期间,发生了另一个异常:

回溯(最近通话最近):   发送中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ requests \ adapters.py”,行449     超时=超时   文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”,第638行,位于urlopen中     _stacktrace = sys.exc_info()[2])   文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ util \ retry.py”,行367,增量     提高六点.reraise(type(error),error,_stacktrace)   重新列出文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ packages \ six.py”,行685     提高价值.with_traceback(tb)   文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”,行600,在urlopen中     分块=块状)   _make_request中的第343行的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”     self._validate_conn(conn)   _validate_conn中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connectionpool.py”,行839     conn.connect()   连接中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ connection.py”,第344行     ssl_context =上下文)   ssl_wrap_socket中的第344行的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ urllib3 \ util \ ssl_.py”     返回context.wrap_socket(sock,server_hostname = server_hostname)   wrap_socket中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行412     会话=会话   _create中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行850     self.do_handshake()   在do_handshake中的文件“ C:\ Users \ Administrator \ AppData \ Local \ Programs \ Python \ Python37 \ lib \ ssl.py”,行1108     self._sslobj.do_handshake() urllib3.exceptions.ProtocolError:('连接异常终止。',OSError(0,'Error'))

在处理上述异常期间,发生了另一个异常:

回溯(最近通话最近):   在第15行的文件“ C:/Users/Administrator/PycharmProjects/untitled5/translatetest.py”     print(u'Text:{}'。format(text))   文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ google \ cloud \ translate_v2 \ client.py”,第233行,翻译成     method ='POST',path ='',data = data)   api_request中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ google \ cloud_http.py”,第290行     标头=标头,target_object = _target_object)   _make_request中的第183行的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ google \ cloud_http.py”     返回self._do_request(方法,URL,标头,数据,target_object)   _do_request中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ google \ cloud_http.py”,第212行     url = url,method = method,headers = headers,data = data)   请求中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ google \ auth \ transport \ requests.py”,在请求中     方法,网址,数据=数据,标头= request_headers,** kwargs)   请求中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ requests \ sessions.py”,行533     resp = self.send(准备,** send_kwargs)   发送文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ requests \ sessions.py”,行646     r = adapter.send(request,** kwargs)   发送中的文件“ C:\ Users \ Administrator \ PycharmProjects \ untitled5 \ venv \ lib \ site-packages \ requests \ adapters.py”,行498     引发ConnectionError(err,request = request) requests.exceptions.ConnectionError :(“连接已中止。”,OSError(0,“错误”))

0 个答案:

没有答案