autodetect proxy setting linux

2019-07-02 12:00发布

I am writing a python app that needs to send and retrieve some information from Internet. I would like to auto-detect the proxy setting (to avoid asking the user to set up the proxy configuration). It seems that urllib can do this on Windows and Mac OsX and not on Unix/Linux.

I need/prefer to use the mechanize module, instead of urllib/urllib2. (It is easier to handle data encoded as "multipart/form-data).

Can the mechanize module auto-detect the proxy setting? If true, it will work on Windows, Mac OsX and Linux?

The following code does not work (I am behind a proxy on Linux), unless I uncomment the fourth line.

import mechanize

br = mechanize.Browser()
#br.set_proxies({'http': 'myproxy.com:3128'})
br.open('http://www.google.com')
response = br.geturl()
print response

I guess this means that mechanize can´t auto-detect proxy setting (or may be I am doing something wrong)

How can I auto-detect the proxy setting on Linux (using python)?

EDIT: added on 9th september

I could confirm that Mechanize autodetects proxy setting on Windows, but not on Linux. As mru correctly pointed out there is no standardized way under Linux to determine the proxy, so I guess the best solution is to check if the user is using Linux and In that case try to get the proxy settings from http_proxy environment variable or from gconf (for Gnome) or from kioslaverc (KDE). And if everything fails I will ask the user to provided the correct proxy settings (I think this is a fair solution because on one hand I think most Linux users know what a proxy is and on the other hand at least I tried to make things easier for them :-) )

1条回答
干净又极端
2楼-- · 2019-07-02 12:56

One way is to check the HTTP_PROXY environment variable (that's the way wget checks if it has to use a proxy). The code could look like this for example:

import os
import mechanize

br = mechanize.Browser()

proxy = os.environ.get('HTTP_PROXY')
if proxy is not None:
    br.set_proxies({'http': proxy})

br.open('http://www.google.com')
response = br.geturl()
print response

But this won't work on Windows (I don't know for MacOS as it's UNIX based).

查看更多
登录 后发表回答