错误:503 DNS解析在Google Translate API中失败,但仅在通过终端执行Python文件时失败



使用带有JSON授权密钥的Google Translate API时遇到了一个奇怪的问题。我可以直接在我选择的编辑器(VSCode(中运行该文件而没有任何问题。没有错误。

但当我通过终端运行同一个文件时,该文件会一直执行到从磁盘加载谷歌翻译凭据之后。然后调用抛出以下错误消息。

由于这种情况只有在从终端运行文件时才会发生,我发现这个错误很难解决。

目标是让这个文件收集数据,并使用谷歌服务翻译一些字段,然后将数据存储在数据库中。

以下是错误消息:

Error: 503 DNS resolution failed
Traceback (most recent call last):
File "/home/MY-USER-NAME/anaconda3/lib/python3.6/site-packages/google/api_core/grpc_helpers.py", line 57, in error_remapped_callable
File "/home/MY-USER-NAME/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 690, in __call__
File "/home/MY-USER-NAME/anaconda3/lib/python3.6/site-packages/grpc/_channel.py", line 592, in _end_unary_response_blocking
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with:
status = StatusCode.UNAVAILABLE
details = "DNS resolution failed"
debug_error_string = "{"created":"@1584629013.091398712","description":"Failed to pick subchannel","file":"src/core/ext/filters/client_channel/client_channel.cc","file_line":3934,"referenced_errors":[{"created":"@1584629013.091395769","description":"Resolver transient failure","file":"src/core/ext/filters/client_channel/resolving_lb_policy.cc","file_line":262,"referenced_errors":[{"created":"@1584629013.091394954","description":"DNS resolution failed","file":"src/core/ext/filters/client_channel/resolver/dns/c_ares/dns_resolver_ares.cc","file_line":370,"grpc_status":14,"referenced_errors":[{"created":"@1584629013.091389655","description":"C-ares status is not ARES_SUCCESS: Could not contact DNS servers","file":"src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc","file_line":244,"referenced_errors":[{"created":"@1584629013.091380513","description":"C-ares status is not ARES_SUCCESS: Could not contact DNS servers","file":"src/core/ext/filters/client_channel/resolver/dns/c_ares/grpc_ares_wrapper.cc","file_line":244}]}]}]}]}"
>

这是我代码的相关部分:

  1. 专用CRON文件(我在许多其他项目中使用这种两步设置,没有任何问题(
#! anaconda3/bin/python
import os 
os.chdir(r'/home/MY-USER-NAME/path-to-project')
import code-file
  1. 代码文件(简单.py脚本(

[...]
from google.oauth2 import service_account
from google.cloud import translate
key_path = 'path-to-file/credentials.json'
credentials = service_account.Credentials.from_service_account_file(
key_path, scopes=['https://www.googleapis.com/auth/cloud-platform'])
def translate_to_en(contents, credentials=None, verbose=False, use_pinyin=False):
[...]
client = translate.TranslationServiceClient(credentials=credentials)  
parent = client.location_path(credentials.project_id, 'global')
[...]
response = client.translate_text(
parent=parent,
contents=[contents],
mime_type='text/plain',  # mime types: text/plain, text/html
target_language_code='en-US')
[...]

[...]
for c in trans_cols:
df[f'{c}__trans'] = df[c].apply(lambda x: translate_to_en(contents=x, credentials=credentials, verbose=False))
[...]

如果有人有一个好主意来解决这个问题,我们将非常感谢您的帮助。

这个问题似乎与github中报告的grpc错误有关。对于gRPC名称解析,您可以在github上遵循此文档。

最新更新