自动检测代理设置Linux [英] autodetect proxy setting linux

查看:151
本文介绍了自动检测代理设置Linux的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Python应用程序,该应用程序需要从Internet发送和检索一些信息.我想自动检测代理设置(以避免要求用户设置代理配置).看来 urllib 可以在Windows和Mac OsX上实现,而不能在Unix/Linux上.

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.

我需要/更喜欢使用机械化模块,而不是urllib/urllib2. (更容易处理编码为"multipart/form-data"的数据.

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

机械化模块可以自动检测代理设置吗?如果为true,它将在Windows,Mac OsX和Linux上运行吗?

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

除非我取消注释第四行,否则以下代码不起作用(我在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)

如何在Linux上自动检测代理设置(使用python)?

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

添加于9月9日

我可以确认Mechanize在Windows上自动检测代理设置,但在Linux上不是. 正如 mru 正确指出的那样,Linux下没有确定代理的标准化方法,所以我认为最好的解决方案是检查如果用户使用的是Linux,在这种情况下,请尝试从http_proxy环境变量或gconf(对于Gnome)或kioslaverc(KDE)获取代理设置.而且,如果一切失败,我将要求用户提供正确的代理设置(我认为这是一个公平的解决方案,因为一方面,我认为大多数Linux用户都知道代理是什么,另一方面,我至少试图使事情变得更容易.为他们:-))

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 :-) )

推荐答案

一种方法是检查HTTP_PROXY环境变量(这是wget检查它是否必须使用代理的方式).该代码可能看起来像这样:

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

但这在Windows上不起作用(我不知道MacOS是基于UNIX的.)

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

这篇关于自动检测代理设置Linux的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆