A tutorial on using Python to make Internet Explorer a proxy

Source: Internet
Author: User
Factory in the last network need to set up a proxy server, switch various environments "including but not limited to development environment, QA, pre-launch, acceptance, production environment, stress testing, Demo ..." All need to set a different proxy server for the browser.

Although I have artifact Firefox+change host+hostadmin+proxy selector combination to easily switch Host, switch browser agent, but ... All are afraid of "but".

But encountered some IE only bug when you have to change the browser Ah!! Also open the virtual machine to engage in IE6, IE8, 360, Sogou these wonderful browser Ah!!!

Some colleagues suggest a bat script to do this, but no one is willing to do it ... And bat can not be realized first not to say, the point is that we are not familiar ah.

Having a C # write a WinForm or console console also requires the. NET Framework, a virtual machine that has a. NET framework4.0 and a lot of time "and the different snapshot are going to be loaded again ..."

The most important, long time not to write the article is not, I do not want to write in the blog C # related things are not. So, fencing python to write a few lines of code and factory Brothers to show off "life is too short, I use Python" benefits.

The specific implementation steps are as follows:

Install Pywin32, WMI support. Specific download address Google, because my is the 32-bit python2.7 series, downloaded to the file name (Pywin32-218.win32-py2.7.exe, Wmi-1.4.7.win32.exe)

Open up.

First of all, we check the information that the agent content of IE browser in the registry "Hkeycurrentuser\software\microsoft\windows\currentversion\internet Settings" is stored here, So we theoretically just change the relevant key value here can switch IE Proxy.

So, the first function is to modify the registry key value:

def changeieproxy (KeyName, KeyValue):  pathinreg = ' Software\Microsoft\Windows\CurrentVersion\Internet Settings '  key = Win32API. RegOpenKey (Win32con. Hkey_current_user,pathinreg, 0, Win32con. key_all_access)  Win32API. RegSetValueEx (key, KeyName, 0, Win32con. REG_SZ, KeyValue)  Win32API. RegCloseKey (Key)

So the code used in the Pywin32 of things, so at the beginning of the file need to do import Win32API, Win32con, introduce the relevant class

Functions that modify the system registry are actually just a few lines ... Of course, because my factory must be online through proxy server, so modify the system registry key value type I only use the REG_SZ this one, the actual other situation will have REG_DWORD ah and so on type.

Then we need a configuration file to save various scenarios "QA AH development environment AH" Different configuration information, this time I use the configuration file is the INI format, with Python's own configparser can parse the file format.

Without using the XML or JSON that I used to be most familiar with, just to pretend that x,xml and JSON are all about the web, the INI looks more like a more commonly used configuration file format than an. exe.

Also because previously did not use the INI format configuration file, this time the right to learn a python play.

So the code to read the INI configuration file is:

Config = Configparser.configparser () config.read (' Config.ini ') if Config.has_section (_section):  _proxyserver = Config.get (_section, ' ProxyServer ')  _proxyoverride = Config.get (_section, ' ProxyOverride ')

Similarly, because of the use of configparser, you need to import configparser at the beginning of the file.

Careful little partner will notice that there is a _section variable in this code is actually not defined, and this variable I give it is written in front of the "scene", such as _section== ' Dev ' represents the development environment, _section== ' QA ' represents the QA environment, and since we are doing an EXE-like program, _section needs to be passed in as a parameter when executing exe.

At this point we will use the Python sys module, the same import sys, and then in the program through:

_section = sys.argv[1] If len (SYS.ARGV) > 1 Else ' dev '

This way to get the "scene" parameter, this section of code will become:

_section = sys.argv[1] If len (SYS.ARGV) > 1 Else ' dev ' config = configparser.configparser () config.read (' Config.ini ') if Config.has_section (_section):  _proxyserver = Config.get (_section, ' ProxyServer ')  _proxyoverride = Config.get (_section, ' ProxyOverride ')

Now that you have read the ProxyServer and ProxyOverride in the configuration file, writing to the registry can theoretically complete our modification of the IE proxy configuration:

_section = sys.argv[1] If len (SYS.ARGV) > 1 Else ' dev ' config = configparser.configparser () config.read (' Config.ini ') if Config.has_section (_section):  _proxyserver = Config.get (_section, ' ProxyServer ')  _proxyoverride = Config.get (_section, ' ProxyOverride ')  changeieproxy (' ProxyServer ', _proxyserver)  changeieproxy (' ProxyOverride ', _proxyoverride)

Why is the previous sentence "Theoretically", because the registry content has been modified, but in fact, IE browser does not take effect, so that the Internet Explorer will need to turn off reopen.

This is the use of an east-front installation called WMI, import WMI cTYPES, and then:

Def kill_ie ():  C = WMI. WMI ()  kernel32 = Ctypes.windll.kernel32 for  process in c.win32_process ():    if process. name== ' iexplore.exe ':      kernel32. TerminateProcess (KERNEL32. OpenProcess (1, 0, process. ProcessId), 0)

Of course, this code is a little bit of a problem, only turned off IE did not reopen ... Because I am lazy, I can accept the manual open ie ...

Sum up:

The complete code is:

#coding =utf-8import Win32API, Win32con, sys, configparser, OS, WMI, Ctypesdef Kill_ie (): c = WMI. WMI () kernel32 = Ctypes.windll.kernel32 for process in c.win32_process (): if process. name== ' iexplore.exe ': kernel32. TerminateProcess (KERNEL32. OpenProcess (1, 0, process. ProcessId), 0) def changeieproxy (KeyName, keyValue): Pathinreg = ' Software\Microsoft\Windows\CurrentVersion\Internet Settings ' key = Win32API. RegOpenKey (Win32con. Hkey_current_user,pathinreg, 0, Win32con. key_all_access) Win32API. RegSetValueEx (key, KeyName, 0, Win32con. REG_SZ, KeyValue) Win32API.    RegCloseKey (Key) def check_config (): If not os.path.isfile (' Config.ini '): cfg = Configparser.configparser () #开发环境 Cfg.add_section (' Dev ') cfg.set (' Dev ', ' proxyserver ', ' 192.168.0.6:3128 ') cfg.set (' Dev ', ' proxyoverride ', ' localhost 127.0.0.1 ') #预上线 cfg.add_section (' Prepare ') cfg.set (' Prepare ', ' proxyserver ', ' 192.168.0.6:3128 ') cfg.set (' PR    Epare ', ' proxyoverride ', ' localhost;127.0.0.1; ') #线上 CFg.add_section (' online ') cfg.set (' online ', ' proxyserver ', ' 192.168.0.6:3128 ') cfg.set (' online ', ' proxyoverride ', ' Lo calhost;127.0.0.1 ') #QA cfg.add_section (' QA ') cfg.set (' QA ', ' proxyserver ', ' 192.168.2.16:3128 ') cfg.set (' QA '), ' ProxyOverride ', ' localhost;127.0.0.1 ') cfg.write (open (' Config.ini ', ' a ')) return False return trueif __name__ = = " __main__ ": _section = sys.argv[1] If len (SYS.ARGV) > 1 Else ' dev ' if Check_config (): kill_ie () config = CONFIGP Arser. Configparser () config.read (' Config.ini ') if Config.has_section (_section): _proxyserver = Config.get (_section, '      ProxyServer ') _proxyoverride = Config.get (_section, ' ProxyOverride ') changeieproxy (' ProxyServer ', _ProxyServer) Changeieproxy (' ProxyOverride ', _proxyoverride) print ' done, open ie ' else:print ' config.ini are created, modif Y Config.ini and try Again '
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.