MCPcopy Index your code
hub / github.com/RustPython/RustPython / do_open

Method do_open

Lib/urllib/request.py:1280–1344  ·  view source on GitHub ↗

Return an HTTPResponse object for the request, using http_class. http_class must implement the HTTPConnection API from http.client.

(self, http_class, req, **http_conn_args)

Source from the content-addressed store, hash-verified

1278 return request
1279
1280 def do_open(self, http_class, req, **http_conn_args):
1281 """Return an HTTPResponse object for the request, using http_class.
1282
1283 http_class must implement the HTTPConnection API from http.client.
1284 """
1285 host = req.host
1286 if not host:
1287 raise URLError('no host given')
1288
1289 # will parse host:port
1290 h = http_class(host, timeout=req.timeout, **http_conn_args)
1291 h.set_debuglevel(self._debuglevel)
1292
1293 headers = dict(req.unredirected_hdrs)
1294 headers.update({k: v for k, v in req.headers.items()
1295 if k not in headers})
1296
1297 # TODO(jhylton): Should this be redesigned to handle
1298 # persistent connections?
1299
1300 # We want to make an HTTP/1.1 request, but the addinfourl
1301 # class isn't prepared to deal with a persistent connection.
1302 # It will try to read all remaining data from the socket,
1303 # which will block while the server waits for the next request.
1304 # So make sure the connection gets closed after the (only)
1305 # request.
1306 headers["Connection"] = "close"
1307 headers = {name.title(): val for name, val in headers.items()}
1308
1309 if req._tunnel_host:
1310 tunnel_headers = {}
1311 proxy_auth_hdr = "Proxy-Authorization"
1312 if proxy_auth_hdr in headers:
1313 tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr]
1314 # Proxy-Authorization should not be sent to origin
1315 # server.
1316 del headers[proxy_auth_hdr]
1317 h.set_tunnel(req._tunnel_host, headers=tunnel_headers)
1318
1319 try:
1320 try:
1321 h.request(req.get_method(), req.selector, req.data, headers,
1322 encode_chunked=req.has_header('Transfer-encoding'))
1323 except OSError as err: # timeout error
1324 raise URLError(err)
1325 r = h.getresponse()
1326 except:
1327 h.close()
1328 raise
1329
1330 # If the server does not send us a 'Connection: close' header,
1331 # HTTPConnection assumes the socket should be left open. Manually
1332 # mark the socket to be closed when this response object goes away.
1333 if h.sock:
1334 h.sock.close()
1335 h.sock = None
1336
1337 r.url = req.get_full_url()

Callers 8

test_httpMethod · 0.95
test_http_closedMethod · 0.95
test_invalid_closedMethod · 0.95
http_openMethod · 0.80
https_openMethod · 0.80
http_openMethod · 0.80
http_openMethod · 0.80
https_openMethod · 0.80

Calls 12

URLErrorClass · 0.90
get_full_urlMethod · 0.80
set_debuglevelMethod · 0.45
updateMethod · 0.45
itemsMethod · 0.45
titleMethod · 0.45
set_tunnelMethod · 0.45
requestMethod · 0.45
get_methodMethod · 0.45
has_headerMethod · 0.45
getresponseMethod · 0.45
closeMethod · 0.45

Tested by 6

test_httpMethod · 0.76
test_http_closedMethod · 0.76
test_invalid_closedMethod · 0.76
http_openMethod · 0.64
https_openMethod · 0.64
http_openMethod · 0.64