Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python try/except不捕获请求异常。get()_Python - Fatal编程技术网

Python try/except不捕获请求异常。get()

Python try/except不捕获请求异常。get(),python,Python,我在Raspberry Pi上使用Python 3.4将天气数据上传到网站。有时上传时出现问题(网速慢或其他),我的程序崩溃。我正在使用try/except,但由于某些原因,它无法捕获错误。我认为最后一个except语句应该捕获任何其他错误 以下是错误: Traceback (most recent call last): File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 343, in

我在Raspberry Pi上使用Python 3.4将天气数据上传到网站。有时上传时出现问题(网速慢或其他),我的程序崩溃。我正在使用try/except,但由于某些原因,它无法捕获错误。我认为最后一个except语句应该捕获任何其他错误

以下是错误:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 343, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 839, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connection.py", line 344, in connect
    ssl_context=context)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/ssl_.py", line 344, in ssl_wrap_socket
    return context.wrap_socket(sock, server_hostname=server_hostname)
  File "/usr/lib/python3.4/ssl.py", line 364, in wrap_socket
    _context=self)
  File "/usr/lib/python3.4/ssl.py", line 577, in __init__
    self.do_handshake()
  File "/usr/lib/python3.4/ssl.py", line 804, in do_handshake
    self._sslobj.do_handshake()
socket.timeout: _ssl.c:584: The handshake operation timed out

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.4/dist-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/packages/six.py", line 686, in reraise
    raise value
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._raise_timeout(err=e, url=url, timeout_value=conn.timeout)
  File "/usr/local/lib/python3.4/dist-packages/urllib3/connectionpool.py", line 306, in _raise_timeout
    raise ReadTimeoutError(self, url, "Read timed out. (read timeout=%s)" % timeout_value)
urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='rtupdate.wunderground.com', port=443): Read timed out. (read timeout=5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Weather_Station/WU_upload.py", line 52, in upload2WU
    r = requests.get(full_URL, timeout=5) # send data to WU
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 75, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/api.py", line 60, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 533, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/sessions.py", line 646, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/requests/adapters.py", line 529, in send
    raise ReadTimeout(e, request=request)
requests.exceptions.ReadTimeout: HTTPSConnectionPool(host='rtupdate.wunderground.com', port=443): Read timed out. (read timeout=5)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/Weather_Station/Weather_Station.py", line 381, in <module>
    uploadStatus = WU_upload.upload2WU(suntec, WU_STATION)
  File "/home/pi/Weather_Station/WU_upload.py", line 65, in upload2WU
    except requests.exceptions.NewConnectionError:
AttributeError: 'module' object has no attribute 'NewConnectionError'
>>> 

请仔细阅读错误消息-尤其是关于
AttributeError
的部分在
请求中没有
NewConnectionError
异常。异常
。顺便说一句,不要单独使用
Exception
:至少要使用
Exception
。再见,谢谢。我删除了
NewConnectionError
并为
Timeout
添加了一个,程序捕获超时错误并继续运行。
try:
    r = requests.get(full_URL, timeout=5) # send data to WU

    # If uploaded successfully, website will reply with 200
    if r.status_code == 200:
        return(True)
    else:
        print('Upload Error: {}  {}'.format(r.status_code, r.text))
        return(False)

except requests.exceptions.ConnectionError:
    print("Upload Error in upload2WU() - ConnectionError")
    return(False)

except requests.exceptions.NewConnectionError:
    print("Upload Error in upload2WU() - NewConnectionError")
    return(False)

except requests.exceptions.ReadTimeout:
    print("Upload Error in upload2WU() - ReadTimeout")
    return(False)

except requests.exceptions.MaxRetryError:
    print("Upload Error in upload2WU() - MaxRetryError")
    return(False)

except socket.gaierror:
    print("Upload Error in upload2WU() - socket.gaierror")
    return(False)

except:
    print("Upload Error in upload2WU() - other")
    return(False)