在Python更改浏览器代理设置? [英] Change browser proxy settings from Python?

查看:2200
本文介绍了在Python更改浏览器代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了依赖于代理的功能的程序。我现在需要一个脚本,将检查是否浏览器设置为使用正确的代理服务器,如果不是,将其更改为使用它。我需要这个实施了许多浏览器越好,但只用于 IE浏览器,谷歌Chrome,Mozilla Firefox浏览器,Safari和Opera 。我什至不知道如何去了解这一点,但它是工作的一个项目,就会因在几天之内。如果有人可以帮助或给予意见,我将不胜AP preciate呢!

I have written a program that relies on a proxy to function. I now need a script that will check to see if the browser is set to use the right proxy, and if not, change it to use it. I need this implemented for as many browsers as possible, but is only required for Internet Explorer, Google Chrome, Mozilla Firefox, Safari and Opera. I am not even sure how to go about this, but it is for a project for work that will be due in a few days. If anyone can help or lend advice, I would greatly appreciate it!

我编程上:结果
微软Windows XP结果
Python的2.6

I am programming on:
MS Windows XP
Python 2.6

推荐答案

在Windows存储其系统范围的代理在注册表中,看在该 HKEY_CURRENT_USER \\软件\\微软\\的Windows \\ CurrentVersion \\ Internet设置。您可以使用Python _winreg模块来改变它(或者,如果你使用Python 3只WinReg项)。下面是一个示例code

The Windows stores its system wide proxy in the registry, look in the the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings. You can use the Python _winreg module to change it (or just winreg if you use Python 3). Here is a sample code

import _winreg as winreg

INTERNET_SETTINGS = winreg.OpenKey(winreg.HKEY_CURRENT_USER,
    r'Software\Microsoft\Windows\CurrentVersion\Internet Settings',
    0, winreg.KEY_ALL_ACCESS)

def set_key(name, value):
    _, reg_type = winreg.QueryValueEx(INTERNET_SETTINGS, name)
    winreg.SetValueEx(INTERNET_SETTINGS, name, 0, reg_type, value)

set_key('ProxyEnable', 1)
set_key('ProxyOverride', u'*.local;<local>')  # Bypass the proxy for localhost
set_key('ProxyServer', u'X.X.X.X:8080')

要禁用它,你可以只需要ProxyEnable键设置为0:

To disable it you can just need to set ProxyEnable key to 0:

set_key('ProxyEnable', 0)

脚本运行后的浏览器仍然有存储在内存中的旧代理,所以您需要重新启动他们,使他们可以重新从注册表中读取新的代理设置。我发现这是非常讨厌,所以我转换<一个href=\"http://stackoverflow.com/questions/2020363/how-to-change-global-windows-proxy-using-c-net-with-immediate-effect\">this片段到Python。

import ctypes

INTERNET_OPTION_REFRESH = 37
INTERNET_OPTION_SETTINGS_CHANGED = 39

internet_set_option = ctypes.windll.Wininet.InternetSetOptionW

internet_set_option(0, self.INTERNET_OPTION_REFRESH, 0, 0)
internet_set_option(0, self.INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)

这刷新的Windows设置,因此,所有你所要做的就是按F5键您的浏览器,以便将其抓住新的代理设置。

This refreshes the Windows settings so all you have to do is hit F5 in your browser in order to it grab the new proxy settings.

我写了一个小脚本切换我的代理或关闭,源在到位桶:
https://bitbucket.org/canassa/switch-proxy

I've written a small script to switch my proxy on or off, the source in on Bitbucket: https://bitbucket.org/canassa/switch-proxy

这应与使用Windows系统级代理任何浏览器(例如:浏览器,IE浏览器)工作。比如Firefox浏览器的一些使用内部代理服务器设置。如果你想改变这些,你将不得不弄清楚他们保存他们的设置和写code去改变它。

This should work with any browser that uses the Windows system-wide proxy (e.g.: Chrome, IE). Some browsers like Firefox uses a internal proxy settings. If you want to change these you will have figure out where they store their settings and write code to change it.

这篇关于在Python更改浏览器代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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