Closed dodo5522 closed 7 years ago
import requests.exceptions as exc
dir(exc)
['BaseHTTPError',
'ChunkedEncodingError',
'ConnectTimeout',
'ConnectionError',
'ContentDecodingError',
'FileModeWarning',
'HTTPError',
'InvalidSchema',
'InvalidURL',
'MissingSchema',
'ProxyError',
'ReadTimeout',
'RequestException',
'RequestsWarning',
'RetryError',
'SSLError',
'StreamConsumedError',
'Timeout',
'TooManyRedirects',
'URLRequired',
'__builtins__',
'__cached__',
'__doc__',
'__file__',
'__loader__',
'__name__',
'__package__',
'__spec__']
try:
requests.get("http://aaa.bbb/index.html")
except (exc.ConnectionError, exc.Timeout, exc.ConnectTimeout, exc.HTTPError) as e:
print(type(e).__name__)
print(e)
ConnectionError
HTTPConnectionPool(host='aaa.bbb', port=80): Max retries exceeded with url: /index.html (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x1115ca8d0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known',))
import requests.exceptions as req_exc
try:
requests.get("http://aaa/MBCSV.cgi")
except (req_exc.ConnectTimeout, req_exc.ConnectionError, req_exc.HTTPError, req_exc.ReadTimeout, req_exc.RequestException, req_exc.RetryError, req_exc.SSLError, req_exc.Timeout, req_exc.TooManyRedirects) as e:
# retry to connect
logger.error(type(e).__name__)
のような感じだろうか。
うまくいった。
チャージコントローラとのセッションが切れた後再接続して動作を継続するようになった。
2016/11/12 AM 12:59:35 solar_monitor DEBUG: Counter, Amp Hours, 31667.7[Ah]
2016/11/12 AM 1:09:39 solar_monitor DEBUG: Exception raised at running target function: ConnectTimeout
2016/11/12 AM 1:09:39 solar_monitor DEBUG: Detail: HTTPConnectionPool(host='192.168.1.20', port=80): Max retries exceeded with url: /MBCSV.cgi?ID=1&F=4&AHI=0&ALO=
29&RHI=0&RLO=1 (Caused by ConnectTimeoutError(<requests.packages.urllib3.connection.HTTPConnection object at 0xb05113f0>, 'Connection to 192.168.1.20 timed out. (c
onnect timeout=5)'))
2016/11/12 AM 1:19:35 solar_monitor DEBUG: Battery, Battery Voltage, 23.75244140625[V]
2016/11/12 AM 1:19:35 solar_monitor DEBUG: Array, Array Voltage, 0.3021240234375[V]
2016/11/12 AM 1:19:35 solar_monitor DEBUG: Counter, Kilowatt Hours, 574[kWh]
2016/11/12 AM 1:19:35 solar_monitor DEBUG: Array, Array Current, 0.0[A]