Support http proxy environment variables

If defined, the variables http_proxy, HTTP_PROXY, https_proxy or
HTTPS_PROXY will be used to create a proxied pool manager.

If the prefix https:// is used by the url, it will try to use
the corresponding proxy variables. If not defined, it will
fallback to http_proxy and HTTP_PROXY.

Signed-off-by: Denis A. Altoé Falqueto <denisfalqueto@gmail.com>
This commit is contained in:
Denis A. Altoé Falqueto 2016-11-16 22:11:08 -02:00 committed by Denis Falqueto
parent d4358e113c
commit f734bf298f
2 changed files with 46 additions and 1 deletions

14
buku.1
View File

@ -222,6 +222,20 @@ Exit buku.
.BI BROWSER
Overrides the default browser. Ref:
.I http://docs.python.org/library/webbrowser.html
.TP
.BR http_proxy ", " HTTP_PROXY ", " https_proxy " or " HTTPS_PROXY"
If defined, will be used to access http and https resources through the configured proxy.
For https requests, if https proxy is not defined but http is, the latter will be used
instead.
They should be in the following format:
http[s]://proxyhost:proxyport
Buku uses urllib3 for proxy support, but that library doesn't support authenticated
proxies. So, if that's your need, you should use a local intermediate proxy, such as
cntlm, TinyProxy or Polipo.
.SH EXAMPLES
.PP
.IP 1. 4

33
buku.py
View File

@ -1492,6 +1492,37 @@ def get_page_title(resp):
return htmlparser.parsed_title
def create_poolmanager(url):
'''Creates a pool manager with proxy support, if needed.
Checks to see if there are proxy variables defined and creates
a ProxyManager. If not, creates a PoolManager.
'''
# Check if http_proxy or HTTP_PROXY is defined
http_proxy = os.environ.get('http_proxy')
if not http_proxy:
http_proxy = os.environ.get('HTTP_PROXY')
# Check if https_proxy or HTTPS_PROXY is defined
https_proxy = os.environ.get('https_proxy')
if not https_proxy:
https_proxy = os.environ.get('HTTPS_PROXY')
# If not, falls back to http_proxy, which is more common.
if not https_proxy:
https_proxy = http_proxy
# Create a pool manager with or without proxy support as needed.
if url.startswith('http://') and http_proxy:
manager = urllib3.ProxyManager(http_proxy)
elif url.startswith('https://') and https_proxy:
manager = urllib3.ProxyManager(https_proxy)
else:
manager = urllib3.PoolManager()
return manager;
def network_handler(url):
'''Handle server connection and redirections
@ -1513,7 +1544,7 @@ def network_handler(url):
if not http_handler:
urllib3.disable_warnings()
http_handler = urllib3.PoolManager()
http_handler = create_poolmanager(url)
try:
while True: