Python script to enable IE to implement proxy Internet tutorial

Source: Internet
Author: User
Tags config ini json prepare win32 python script

python script to enable IE to implement proxy Internet tutorial

This article mainly introduces the use of Python script to enable IE to achieve the Internet proxy tutorial, "famous" Goagent agent is also based on the same principle to achieve, the need for friends can refer to the

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 want to 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 "Hkeycurrentusersoftwaremicrosoftwindowscurrentversioninternet 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:

?

1 2 3 4 5 6 7 8 9 def changeieproxy (KeyName, keyvalue): Pathinreg = ' softwaremicrosoftwindowscurrentversioninternet Settings ' key = Win3 2api. 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:

?

1 2 3 4 5 6 7 8 9 Config = Configparser.configparser () config.read (' Config.ini ') if Config.has_section (_section): _proxyserver = Confi G.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:

?

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

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

?

1 2 3 4 5 6 7 8 9 10 11 _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:

?

1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 _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:

?

1 2 3 4 5 6 7 8 9 10 11 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:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5 86 87 88 89 90 91 92 93 94 95 96-97 #coding =utf-8   Import Win32API, Win32con, sys, configparser, OS, WMI, ctypes   def kill_ie ():   C = WMI.W MI ()   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 = ' Softwaremicrosoftwindowscurrentversioninternet 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.CONFIGP Arser ()   #开发环境   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 (' Prepare ', ' proxyoverride ', ' localhost;127.0.0.1; ')   #线上   Cfg.add_section (' online ')   Cfg.set (' online ', ' proxyserver ', ' 192.168.0.6:3128 ')   cfg.set (' online '), ' ProxyOverride ', ' 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.ar GV) > 1 Else ' dev '   if Check_config ():   Kill_ie ()   config = configparser.configparser ()   Config . Read (' Config.ini ')   if Config.has_section (_section):   _proxyserver = Config.get (_section, ' ProxyServer ') & nbsp _proxyoverride = Config.get (_section, ' ProxyOverride ')   changeieproxy (' ProxyServer ', _proxyserver)   Changeieproxy (' ProxyOverride ', _proxyoverride) &nbsp print ' done, 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.