- Notifications
You must be signed in to change notification settings - Fork9.4k
Open
Description
Unsure whether this is the right place to handle this error – happy to report this elsewhere if you disagree.
When opening a request with a malformed URL through a proxy, an AssertionError from deep in the http library is reported.
Expected Result
ConnectionError
,NameResolutionError
or something more indicative of what the problem is
Actual Result
AssertionError: b''
Reproduction Steps
importrequestsrequests.get("http://%C2%A0www.github.com/",proxies=client.proxies)^[[H---------------------------------------------------------------------------AssertionErrorTraceback (mostrecentcalllast)CellIn[36],line1---->1requests.get("http://%C2%A0www.github.com/",proxies=client.proxies)File/opt/venv/lib/python3.12/site-packages/requests/api.py:73,inget(url,params,**kwargs)62defget(url,params=None,**kwargs):63r"""Sends a GET request. 64 65 :param url: URL for the new :class:`Request` object. (...) 70 :rtype: requests.Response 71 """--->73returnrequest("get",url,params=params,**kwargs)File/opt/venv/lib/python3.12/site-packages/requests/api.py:59,inrequest(method,url,**kwargs)55# By using the 'with' statement we are sure the session is closed, thus we56# avoid leaving sockets open which can trigger a ResourceWarning in some57# cases, and look like a memory leak in others.58withsessions.Session()assession:--->59returnsession.request(method=method,url=url,**kwargs)File/opt/venv/lib/python3.12/site-packages/requests/sessions.py:589,inSession.request(self,method,url,params,data,headers,cookies,files,auth,timeout,allow_redirects,proxies,hooks,stream,verify,cert,json)584send_kwargs= {585"timeout":timeout,586"allow_redirects":allow_redirects,587 }588send_kwargs.update(settings)-->589resp=self.send(prep,**send_kwargs)591returnrespFile/opt/venv/lib/python3.12/site-packages/requests/sessions.py:703,inSession.send(self,request,**kwargs)700start=preferred_clock()702# Send the request-->703r=adapter.send(request,**kwargs)705# Total elapsed time of the request (approximately)706elapsed=preferred_clock()-startFile/opt/venv/lib/python3.12/site-packages/requests/adapters.py:667,inHTTPAdapter.send(self,request,stream,timeout,verify,cert,proxies)664timeout=TimeoutSauce(connect=timeout,read=timeout)666try:-->667resp=conn.urlopen(668method=request.method,669url=url,670body=request.body,671headers=request.headers,672redirect=False,673assert_same_host=False,674preload_content=False,675decode_content=False,676retries=self.max_retries,677timeout=timeout,678chunked=chunked,679 )681except (ProtocolError,OSError)aserr:682raiseConnectionError(err,request=request)File/opt/venv/lib/python3.12/site-packages/urllib3/connectionpool.py:789,inHTTPConnectionPool.urlopen(self,method,url,body,headers,retries,redirect,assert_same_host,timeout,pool_timeout,release_conn,chunked,body_pos,preload_content,decode_content,**response_kw)786response_conn=connifnotrelease_connelseNone788# Make the request on the HTTPConnection object-->789response=self._make_request(790conn,791method,792url,793timeout=timeout_obj,794body=body,795headers=headers,796chunked=chunked,797retries=retries,798response_conn=response_conn,799preload_content=preload_content,800decode_content=decode_content,801**response_kw,802 )804# Everything went great!805clean_exit=TrueFile/opt/venv/lib/python3.12/site-packages/urllib3/connectionpool.py:495,inHTTPConnectionPool._make_request(self,conn,method,url,body,headers,retries,timeout,chunked,response_conn,preload_content,decode_content,enforce_content_length)492# conn.request() calls http.client.*.request, not the method in493# urllib3.request. It also calls makefile (recv) on the socket.494try:-->495conn.request(496method,497url,498body=body,499headers=headers,500chunked=chunked,501preload_content=preload_content,502decode_content=decode_content,503enforce_content_length=enforce_content_length,504 )506# We are swallowing BrokenPipeError (errno.EPIPE) since the server is507# legitimately able to close the connection after sending a valid response.508# With this behaviour, the received response is still readable.509exceptBrokenPipeError:File/opt/venv/lib/python3.12/site-packages/urllib3/connection.py:403,inHTTPConnection.request(self,method,url,body,headers,chunked,preload_content,decode_content,enforce_content_length)401skip_accept_encoding="accept-encoding"inheader_keys402skip_host="host"inheader_keys-->403self.putrequest(404method,url,skip_accept_encoding=skip_accept_encoding,skip_host=skip_host405 )407# Transform the body into an iterable of sendall()-able chunks408# and detect if an explicit Content-Length is doable.409chunks_and_cl=body_to_chunks(body,method=method,blocksize=self.blocksize)File/opt/venv/lib/python3.12/site-packages/urllib3/connection.py:347,inHTTPConnection.putrequest(self,method,url,skip_host,skip_accept_encoding)342ifmatch:343raiseValueError(344f"Method cannot contain non-token characters{method!r} (found at least{match.group()!r})"345 )-->347returnsuper().putrequest(348method,url,skip_host=skip_host,skip_accept_encoding=skip_accept_encoding349 )File/usr/local/lib/python3.12/http/client.py:1214,inHTTPConnection.putrequest(self,method,url,skip_host,skip_accept_encoding)1212exceptUnicodeEncodeError:1213netloc_enc=netloc.encode("idna")->1214self.putheader('Host',_strip_ipv6_iface(netloc_enc))1215else:1216ifself._tunnel_host:File/usr/local/lib/python3.12/http/client.py:179,in_strip_ipv6_iface(enc_name)177enc_name,percent, _=enc_name.partition(b"%")178ifpercent:-->179assertenc_name.startswith(b'['),enc_name180enc_name+=b']'181returnenc_nameAssertionError:b''
System Information
$ python -m requests.help
"chardet": { "version": null }, "charset_normalizer": { "version": "3.4.0" }, "cryptography": { "version": "" }, "idna": { "version": "3.10" }, "implementation": { "name": "CPython", "version": "3.12.7" }, "platform": { "release": "6.8.0-36-generic", "system": "Linux" }, "pyOpenSSL": { "openssl_version": "", "version": null }, "requests": { "version": "2.32.3" }, "system_ssl": { "version": "1010117f" }, "urllib3": { "version": "2.2.3" }, "using_charset_normalizer": true, "using_pyopenssl": false}
Metadata
Metadata
Assignees
Labels
No labels