【发布时间】:2020-07-02 17:23:30
【问题描述】:
我有一个 Python 脚本,我想每天使用 cron 执行该脚本。我唯一遇到问题的脚本是它使用 URLLib 请求的脚本,那里出现连接超时错误:
Traceback(最近一次通话最后一次):
File "/usr/lib64/python3.6/urllib/request.py", line 1318, in do_open
encode_chunked=req.has_header('Transfer-encoding'))
File "/usr/lib64/python3.6/http/client.py", line 1254, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1300, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1249, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib64/python3.6/http/client.py", line 1036, in _send_output
self.send(msg)
File "/usr/lib64/python3.6/http/client.py", line 974, in send
self.connect()
File "/usr/lib64/python3.6/http/client.py", line 1407, in connect
super().connect()
File "/usr/lib64/python3.6/http/client.py", line 946, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib64/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib64/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
TimeoutError: [Errno 110] Connection timed out
这是 cron 执行:
0 3 * * * /usr/bin/python3.6 "/home/ngabioud/Scripts Python VM/ClickTableau.py"
您知道为什么这不起作用吗?如果我在命令行上手动运行脚本,它可以正常工作。
谢谢!
【问题讨论】:
-
如果可能,请更新错误跟踪。
-
嗨@AzyCrw4282,把整个错误跟踪
-
超时可能有很多可能的原因,可能与您的程序无关。尝试将 urllib.request.urlopen 中的 timeout 参数设置为更大的值。尝试在您的目录中更改
self.timeout,例如/usr/lib/python3.4/urllib/request.py -
谢谢@AzyCrw4282,你知道这是否与cron有关吗?当它尝试与它一起运行时,它总是失败。如果我手动运行它,我没有问题。
-
我将超时设置为 60 秒,但我仍然收到超时错误 @AzyCrw4282