网络断开错误的错误处理

时间:2018-10-29 14:01:00

标签: python

我正在使用pyowm每分钟后获取天气更新。如果我断开互联网连接或互联网连接中断,将会发生什么错误以及如何处理此错误。对于try..except方法,。必须使用什么可能的例外...? 我的代码是...。

import tkinter as tk
from tkinter import *
import pyowm
from PIL import ImageTk, Image


win=tk.Tk()



#frame_for_showing_weather.
weather_frame=Frame(win,bg='black',width='300',height='300')
weather_frame.place(x=0,y=0)

weath=Label(weather_frame,font=('Agency',100),fg='white',bg='black')
weath.place(x=10,y=10)

temp1=''
owm=pyowm.OWM('my api key')

def obs():
    global temp1
    observation = owm.weather_at_place('Barisal,BD')
    w=observation.get_weather()
    temp2=w.get_temperature('celsius')
    if(temp2!=temp1):
        temp1=temp2['temp']
        weath.config(text=str(int(temp1))+chr(176))
        print('Alhamdulillah,Yeah that is worked')
    weath.after(60000,obs)
obs()


win.mainloop()

请有人解释...我是python的新手。

如果我断开连接,则会发生此大错误...

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 384, in _make_request
    six.raise_from(e, None)
  File "<string>", line 2, in raise_from
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 380, in _make_request
    httplib_response = conn.getresponse()
  File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 1197, in getresponse
    response.begin()
  File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 297, in begin
    version, status, reason = self._read_status()
  File "C:\Program Files (x86)\Python35-32\lib\http\client.py", line 258, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "C:\Program Files (x86)\Python35-32\lib\socket.py", line 575, in readinto
    return self._sock.recv_into(b)
socket.timeout: timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\adapters.py", line 445, in send
    timeout=timeout
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\util\retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\packages\six.py", line 686, in reraise
    raise value
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 386, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=read_timeout)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\urllib3\connectionpool.py", line 306, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPConnectionPool(host='api.openweathermap.org', port=80): Read timed out. (read timeout=2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 24, in get_json
    timeout=self.timeout, verify=self.verify_ssl_certs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\requests\adapters.py", line 526, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPConnectionPool(host='api.openweathermap.org', port=80): Read timed out. (read timeout=2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "C:\Program Files (x86)\Python35-32\lib\tkinter\__init__.py", line 596, in callit
    func(*args)
  File "C:\Users\James Bond\Desktop\test.py", line 23, in obs
    observation = owm.weather_at_place('Barisal,BD')
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\webapi25\owm25.py", line 210, in weather_at_place
    _, json_data = self._wapi.cacheable_get_json(uri, params=params)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 44, in cacheable_get_json
    status_code, data = self.get_json(uri, params=params, headers=headers)
  File "C:\Program Files (x86)\Python35-32\lib\site-packages\pyowm\commons\http_client.py", line 30, in get_json
    raise api_call_error.APICallTimeoutError('API call timeouted')
pyowm.exceptions.api_call_error.APICallTimeoutError: Exception in calling OWM web API.

Reason: API call timeouted

Caused by: None

我该如何处理此错误...?

0 个答案:

没有答案