-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Session does not consistently respects proxy environment variables. #5677
Comments
I believe things will work if you use |
Doesn't seem to be the case. def test_respect_proxy_env_on_send_session_prepared_request(self):
session = requests.Session()
# Does not respect the proxy configuration and ignores the proxy
request = requests.Request(
method='GET', url='https://www.google.com/'
)
with override_environ(https_proxy='http://example.com'):
try:
prepared = session.prepare_request(request)
session.send(prepared)
assert False, "The proxy is invalid this request should not be successful."
except requests.exceptions.ProxyError as e:
assert 'Cannot connect to proxy' in str(e) Result:
Reading the code I know the issue is that there is no call to |
Hello, |
The line changed on the PR has the erroneous precedence format you are mentioning, I'll fix that to be consistent. You mention there might be other code paths using an incorrect one as well, can you point to those? And also the docs in which you saw this precedence mentioned? |
After thinking a bit more about it, I guess the problem arises in I know, I should probably rather make a PR with some regression tests to prove my solution. It would take some time though, since I have to get used to the process first. |
Concerning the documentation, I read the section Proxies as if the environment configuration is "overridden" by settings in Python code, and the examples shown suggest the hierarchy I indicated above. |
👋 we're also encountering this issue in our python library. Hope this helps other folks running into this issue, and thanks @mateusduboli for your excellent breakdown with tests, this really helped out our investigation 😄 |
I had the same issue and @hollabaq86's workaround is best I found so far. |
I'll add my cents here. On Windows, the proxy hierarchy is broken too, and since the environment settings also come from the registry, it's nearly impossible to diagnose why the proxies set in the session are not in use. Few users are aware that Requests will fetch settings from the Windows Registry. In https://github.com/psf/requests/blob/main/requests/sessions.py#L758 we can see that environment settings are merged in the requests settings before the session settings are merged. The merge mechanism is conservative and does not overwrite previous settings, so session settings won't overwrite environment settings. IMHO, environment settings should be merged with with session settings then with requests settings. With the current mechanism, environment settings should be merged after the session settings. |
There are inconsistencies when working with proxy environment variables using
Session
objects.When using
Session#request
or verb methods (get
,post
,put
), those work correctly and use the proxy when they are present.However
Session#send(PreparedRequest)
does not respect the variables by default, but does when it handles redirect responses, e.g.303
.I've written a test class to present this behavior.
Expected Result
All methods in
Session
to respect the proxy variable.Actual Result
Session#send(PreparedRequest)
does not respect proxy environment variables.Reproduction Steps
Test case 1 fails with a successful request.
System Information
The text was updated successfully, but these errors were encountered: