Skip to content
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

Add support for http_proxy environment variable #529

Closed
wrobell opened this issue Sep 26, 2015 · 22 comments
Closed

Add support for http_proxy environment variable #529

wrobell opened this issue Sep 26, 2015 · 22 comments
Labels

Comments

@wrobell
Copy link

wrobell commented Sep 26, 2015

A lot of software is checking for "http_proxy" env variable and if it exists, then it uses the proxy specified in the variable.

Would it be possible to add appropriate code in aiohttp.request or should each software using aiohttp do it on its own?

wrobell referenced this issue in aeroevan/geotiler Sep 26, 2015
I'm surprised aiohttp doesn't do this already.
@asvetlov
Copy link
Member

I think handling http_proxy environment variable is out of aiohttp duties.
requests library doesn't handle it also for example.

@aeroevan
Copy link

IIRC, requests does handle http{,s}_proxy environment variables:
http://docs.python-requests.org/en/latest/user/advanced/#proxies

@asvetlov
Copy link
Member

@aeroevan that sounds strange because I cannot find http_proxy string neither in request nor in urllib3 source code.

@wrobell
Copy link
Author

wrobell commented Sep 27, 2015

Also, urllib.request does

In addition, if proxy settings are detected (for example, when a *_proxy environment variable like http_proxy is set), ProxyHandler is default installed and makes sure the requests are handled through the proxy.

https://docs.python.org/3/library/urllib.request.html#module-urllib.request

@asvetlov
Copy link
Member

@wrobell urllib is not the best example.
Anyway, I've made decision: proxy environment variables will not be processed by aiohttp

@aeroevan
Copy link

@asvetlov I think they build the environment variable lookup programmatically based on protocol to get HTTPS_PROXY, FTP_PROXY, etc.

@qsz13
Copy link

qsz13 commented Mar 31, 2016

@asvetlov Sorry, but I still don't get it.
Actually, requests use urllib.request.getproxies() to check the env:
https://github.com/kennethreitz/requests/blob/master/requests/compat.py
So, why not add support for the http_proxy in env?

@popravich
Copy link
Member

Hi @qsz13, using proxy settings fetched from env will work in a one single case -- when you use all defaults (I mean with ClientSession() as sess: sess.get/post/etc).
Implicitly changing default connector to other one is bad idea.
Consider following example:
You have a project which fetches web pages parses them and store results in elasticsearch.
Lets say that crawler part depends on http_proxy setting and its ok, but elasticsearch
uses http protocol as well, so elasticsearch client will end up trying to connect to your
local elasticsearch server using the same proxy as fetcher (one from http_proxy env var).
Such cases are really hard to debug. Obvious decision in this case to use another env var no_proxy
with elasticsearch server address and so you end up "programming" your environment instead of
correctly programming your system.
Now replace elasticsearch with any other external service using http as a transport and you'll
stuck with the same problem.
BTW, elasticsearch-py driver uses requests lib and (I'm almost sure) will suffer from this issue.

Anyway, import this: explicit is better than implicit

@asvetlov
Copy link
Member

I don't want to relay on environment variables by default but adding a function like session_from_env() which reads HTTP_PROXY, creates appropriate connector and return a session with this connector does make sense.
Is any volunteer for the task?

@ashald
Copy link

ashald commented Aug 1, 2017

It's a shame that aiohttp is not compatible with a def-facto standard environment variablehttp_proxy that is being used by many standard libraries, languages and platforms to seamlessly introduce a proxy without any code changes. Is there is a chance this can be revisited?

@jettify
Copy link
Member

jettify commented Aug 1, 2017

@ashald could you please describe your use case? why suggested session_from_env will not work for you?

@ashald
Copy link

ashald commented Aug 19, 2017

@jettify well, yeah, indeed, session_from_env better than nothing but the problem here is it has to be used at all to achieve such a behavior. Here is a simple example that I faced last week: I have to use some apps to deal with resources that are available to me at this moment only via a proxy. Those apps are written in different languages such as Golang, C, Python etc. And none of their developers though about proxies when they were developing those apps so they didn't do anything special to support them. But thanks to standard libraries in those languages supporting this "de-facto standard" I was able to make those apps work just by executing while having http_proxy set. That's it, voila. This is a total win-win as on one hand developers didn't have to worry about proxies at all and I was able to use the tools without having to patch them.

@asvetlov
Copy link
Member

-10 for implicit environment variables usage. Period.
If you want to open potential security manhole by inserting untrusted proxy in chain -- please do it explicitly by your code. Don't ask aiohttp for it.

@ashald
Copy link

ashald commented Aug 19, 2017

That's why I said it's a shame such an OSS project is being driven by subjective opinions rather than wide-spread practices and standards. Anyway, thanks for spending your time to explain that this feature is not going to be implemented in aiohttp.

@asvetlov
Copy link
Member

Python itself is driven by subjective opinions of Python Core Developers and it's leader Guido van Rossum especially.
The language design is significantly different from wide-spread practices and standards like Java, C, JavaScript, Go (which are in turn very different languages itself).
Is it a shame?

@wrobell
Copy link
Author

wrobell commented Aug 19, 2017

Java supports HTTP proxy properties (that would be equivalent of the environment variables) -
https://docs.oracle.com/javase/8/docs/technotes/guides/net/proxies.html.

HTTP client in Go language seems to support the http_proxy env variables, look for DefaultTransport description at https://golang.org/pkg/net/http/.

@wrobell
Copy link
Author

wrobell commented Dec 1, 2017

It seems we can simply use now

async with aiohttp.ClientSession(trust_env=True) as session:

to pull proxy configuration from environment variables?

@asvetlov
Copy link
Member

asvetlov commented Dec 1, 2017

Yes, exactly.

@wrobell
Copy link
Author

wrobell commented Dec 1, 2017

This is perfect, thank you.

@kstaniek
Copy link

This works fine unless you don't use no_proxy env which is is ignored. I would suggest to reopen this issue and provide the full fix.

@asvetlov
Copy link
Member

Please make a pull request

@lock
Copy link

lock bot commented Oct 28, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a [new issue] for related bugs.
If you feel like there's important points made in this discussion, please include those exceprts into that [new issue].
[new issue]: https://github.com/aio-libs/aiohttp/issues/new

@lock lock bot added the outdated label Oct 28, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 28, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

8 participants