我的python脚本在没有任何干预的情况下停止工作
我是python新手,我的脚本大约持续3-4个小时,不会保存任何错误消息。是什么导致了这个问题 这是我的密码:我的python脚本在没有任何干预的情况下停止工作,python,python-3.x,ubuntu-14.04,Python,Python 3.x,Ubuntu 14.04,我是python新手,我的脚本大约持续3-4个小时,不会保存任何错误消息。是什么导致了这个问题 这是我的密码: import time import urllib.request import threading def load(): try: content = str(urllib.request.urlopen("[URL]").read()) # do sth with content threading.Timer(0.5,
import time
import urllib.request
import threading
def load():
try:
content = str(urllib.request.urlopen("[URL]").read())
# do sth with content
threading.Timer(0.5, load).start()
except Exception as e:
file = open("Error.txt","w")
file.write(time.strftime("%H:%M:%S\n\n"))
file.write(e.message)
file.close()
threading.Timer(0.5, load).start()
def main(args):
load()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
下面是ubuntu 14.04上的nohup.out文件:
Exception in thread Thread-907:
Traceback (most recent call last):
File "/usr/lib/python3.4/urllib/request.py", line 1182, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.4/http/client.py", line 1125, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1163, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1121, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 951, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 886, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 863, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 512, in create_connection
raise err
File "/usr/lib/python3.4/socket.py", line 503, in create_connection
sock.connect(sa)
OSError: [Errno 101] Network is unreachable
无法访问网络
通常意味着您的计算机存在连接问题。也许这是一个间歇性的、短暂的问题,但你从未尝试去发现它并从中恢复过来
考虑使用类似的方法,或者手动处理
(我还发现您循环获取逻辑的方式相当奇怪。为什么简单的单线程循环对您不起作用?
Expection
->Exception
)。输入错误,请关闭。请将file=open(“Error.txt”,“w”)更改为file=open(“Error.txt”,“a”)模式。日志记录
模块比手动打开文件更有意义,网络无法访问
对我来说似乎不言自明。该网站拒绝您的请求或您的internet已断开