Using Python script to enable IE to implement a course for Internet agent _python

Source: Internet
Author: User
Tags prepare python script

Factory in the last network needs to set proxy server, switching various environments "including but not limited to the development environment, QA, pre-line, acceptance, production environment, stress testing, Demo ..." All need to set a different proxy server to the browser.

Although I have the artifact Firefox+change Host+hostadmin+proxy selector combination to easily switch Host, switch browser proxy, but ... Usually afraid of "but".

But encountered some IE only bug time had to change the browser Ah!! Also open a 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 realize first, the key is that we do not know AH.

A C # to write a WinForm or console console also requires the. NET Framework is not, the virtual machine to install a. NET framework4.0 again a lot of time "and the different snapshot must be loaded again ..."

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

The following steps are implemented:

Install Pywin32, WMI support. Specific download address Google, because I am a 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 look for information that the agent content of IE browser in the registry "Hkeycurrentuser\software\microsoft\windows\currentversion\internet Settings" here to save, So we can switch the IE agent in theory as long as we modify the relevant key values here.

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 paragraph code uses the Pywin32 thing, so at the beginning of the file need to do import Win32API, Win32con, introduce the relevant class

Modify the system registry function in fact, just a few lines ... Of course, because my factory must be through the proxy server to the Internet, 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 I need a configuration file, to save various scenarios "QA AH development environment," the different configuration information, at this time I used the configuration file for the INI format, with Python Configparser can parse this file format.

Not using the XML or JSON I used to be the most familiar. For X,xml and JSON as a whole, it seems to be something that is used on the web, and the INI looks like a more commonly used configuration file format for. exe.

Also because of the previous use of the INI format configuration file, this time the right to learn a python play just.

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 Buddy will notice that there is a _section variable in this code that is actually undefined, and the variable I give it is the "scene" written in front of it, such as _section== ' Dev ' that represents the development environment, _section== ' QA ' represents the QA environment, and since we do this is an EXE-like program, so _section need to execute EXE time as a parameter to pass in.

That's when we're going to use the Python sys module, same as import sys, and then pass in the program:

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

This way to get the "scene" parameter, this piece of code becomes:

_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 accomplish the great cause of our changing 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 the previous sentence is "theoretically", because the registry content has been modified, but in fact, IE browser does not take effect, so that the use of IE browser to turn off reopen.

This is where you use the front-mounted one 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 to shut down IE did not reopen ... Because I am lazy, I can accept to manually open ie ...

Sum up:

The complete code is:

#coding =utf-8 Import Win32API, Win32con, sys, configparser, OS, WMI, cTYPES 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 def changeieproxy (KeyName, keyvalue): Pathinreg = ' Software\Microsoft\Windows\CurrentVersion\Internet S Ettings ' 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 ', ' Proxyoverrid E ', ' localhost;127.0.0.1 ') #预上线 cfg.add_section (' Prepare ') cfg.set (' Prepare ', ' proxyserver ', ' 192.168.0.6:3 128 ') Cfg.set (' Prepare ', ' ProXYoverride ', ' localhost;127.0.0.1; ') #线上 cfg.add_section (' online ') cfg.set (' online ', ' proxyserver ', ' 192.168.0.6:3128 ') cfg.set (' online ', ' Proxy Override ', ' localhost;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 True If __name__ = = "__main__": _section = sys.argv[1] If len (SYS.ARGV) > 1 Else ' dev ' if Check_config () : Kill_ie () config = Configparser.configparser () config.read (' Config.ini ') if Config.has_section (_sect ION): _proxyserver = Config.get (_section, ' proxyserver ') _proxyoverride = Config.get (_section, ' Proxyoverrid E ') changeieproxy (' ProxyServer ', _proxyserver) changeieproxy (' ProxyOverride ', _proxyoverride) print ' Do

 NE, open ie ' else:print ' config.ini is created, modify 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.