Configure autodetect proxy linux

I am writing a python application that needs to send and receive some information from the Internet. I would like to automatically detect proxy server settings (so as not to ask the user to configure the proxy server). It seems that urllib can do this on Windows and Mac OsX, and not on Unix / Linux.

I need / prefer to use a mechanization module instead of urllib / urllib2. (It is easier to process data encoded as "multipart / form-data".

Can a mechanization module automatically detect proxy server settings? If true, will it work on Windows, Mac OsX and Linux?

The following code does not work (I'm behind the 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 mechanization cannot automatically detect proxy settings (or maybe I'm doing something wrong)

How can I automatically detect proxy settings on Linux (using python)?

EDIT: added September 9th

I can confirm that Mechanize automatically detects proxy settings on Windows, but not Linux. Since mru correctly pointed out that Linux does not have a standardized way to define a proxy server, so I think the best solution is to check whether the user uses Linux and In in this case, try setting the proxy server settings from the http_proxy environment variable or from gconf ( for Gnome) or from kioslaverc (KDE). And if everything fails, I will ask the user to provide the correct proxy server settings (I think this is a fair solution, because, on the one hand, I think most Linux users know what a proxy server is, and on the other hand at least I tried to simplify for them :-))

+3
source share
1 answer

One way is to check the HTTP_PROXY environment variable (that the wget path checks if it should use a proxy server). The code might look like this:

 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 will not work on Windows (I do not know for MacOS, since it is based on UNIX).

0
source

Source: https://habr.com/ru/post/1482964/


All Articles