summaryrefslogtreecommitdiffstats
path: root/Lib/urllib/request.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/urllib/request.py')
-rw-r--r--Lib/urllib/request.py25
1 files changed, 14 insertions, 11 deletions
diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py
index a17c868..67c7566 100644
--- a/Lib/urllib/request.py
+++ b/Lib/urllib/request.py
@@ -1170,18 +1170,21 @@ class AbstractHTTPHandler(BaseHandler):
h.set_tunnel(req._tunnel_host, headers=tunnel_headers)
try:
- h.request(req.get_method(), req.selector, req.data, headers)
- except OSError as err: # timeout error
- h.close()
- raise URLError(err)
- else:
+ try:
+ h.request(req.get_method(), req.selector, req.data, headers)
+ except OSError as err: # timeout error
+ raise URLError(err)
r = h.getresponse()
- # If the server does not send us a 'Connection: close' header,
- # HTTPConnection assumes the socket should be left open. Manually
- # mark the socket to be closed when this response object goes away.
- if h.sock:
- h.sock.close()
- h.sock = None
+ except:
+ h.close()
+ raise
+
+ # If the server does not send us a 'Connection: close' header,
+ # HTTPConnection assumes the socket should be left open. Manually
+ # mark the socket to be closed when this response object goes away.
+ if h.sock:
+ h.sock.close()
+ h.sock = None
r.url = req.get_full_url()
# This line replaces the .msg attribute of the HTTPResponse