diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -1143,11 +1143,13 @@ try: h.request(req.get_method(), req.selector, req.data, headers) - r = h.getresponse() # an HTTPResponse instance - except socket.error as err: + except socket.error as err: # timeout error raise URLError(err) finally: - h.close() + try: + r = h.getresponse() # an HTTPResponse instance + except Exception: + h.close() r.url = req.get_full_url() # This line replaces the .msg attribute of the HTTPResponse