Python編寫指令碼使IE實現代理上網的教程

來源:互聯網
上載者:User

Python編寫指令碼使IE實現代理上網的教程

   Python編寫指令碼使IE實現代理上網的教程

        這篇文章主要介紹了用Python編寫指令碼使IE實現代理上網的教程,“著名的”goagent代理也是基於同樣原理實現,需要的朋友可以參考下

  廠裡上個網需要設定Proxy 伺服器,切換各種環境『包括但不僅限於開發環境、QA、預上線、驗收、生產環境、壓力測試、Demo……』都需要給瀏覽器設定不同的Proxy 伺服器。

  雖然俺有神器Firefox+Change Host+HostAdmin+Proxy Selector的組合來輕鬆切換Host,切換瀏覽器代理,但是…凡是就怕『但是』。

  但是碰到一些IE才有的bug時候不得不換瀏覽器啊!!還要開虛擬機器進去搞IE6、IE8、360、搜狗這些奇葩瀏覽器啊!!!

  有同事建議搞個bat指令碼來做這些,但沒人肯動手……而且bat能不能實現先不說,重點是咱不熟啊。

  搞個C#寫個winform或者console控制台還需要.NET framework不是,虛擬機器裝個.NET framework4.0又要很多時間『而且不同的snapshot都要裝一遍…』

  最最重要的,好久不寫文章了不是,咱不想在部落格裡寫C#相關的東西不是。所以,操刀Python寫幾行代碼和廠裡兄弟們顯擺一下『人生苦短,我用Python』的好處。

  具體實現步驟如下:

  安裝pywin32、WMI支援。具體Google一下,因為我的是32位python2.7系列,下載到的檔案名稱分別為(pywin32-218.win32-py2.7.exe、WMI-1.4.7.win32.exe)

  開搞。

  首先,我們查資料知道,IE瀏覽器的代理內容在註冊表中『HKEYCURRENTUSER\Software\Microsoft\Windows\CurrentVersion\Internet Settings』這裡存著,所以我們理論上只要修改這裡相關的索引值就可以切換IE代理。

  所以,第一個函數就是修改註冊表索引值:

  ?

1

2

3

4

5

6

7

8

9

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)

  因此段代碼中用到了pywin32的的東西,所以在檔案最開頭需要做import win32api, win32con,引入相關的class

  修改系統註冊表的函數其實就這麼幾行…當然,因為我廠必須通過Proxy 伺服器上網,所以修改系統註冊表的索引值類型我只用到了REG_SZ這一種,實際其他情況還會有REG_DWORD啊等等類型。

  然後咱需要一個設定檔,來儲存各種情境『QA啊開發環境啊』的不同的配置資訊,這時候我使用的設定檔為ini格式,用Python內建的ConfigParser就可以解析此種檔案格式。

  沒有採用以往我最熟悉的XML或者json純粹為了裝x,xml和json總覺著是web上用的東西,ini看起來比較像一個.exe比較常用的設定檔格式。

  也因為以前沒用過ini格式的設定檔,這次權當又學會一種Python的玩法而已。

  所以讀取ini設定檔的代碼為:

  ?

1

2

3

4

5

6

7

8

9

config = ConfigParser.ConfigParser()

 

config.read('config.ini')

 

if config.has_section(_section):

 

_ProxyServer = config.get(_section, 'ProxyServer')

 

_ProxyOverride = config.get(_section, 'ProxyOverride')

  同樣,因為用到了ConfigParser,需要在檔案最開頭也import ConfigParser一下。

  細心的小夥伴會注意到這段代碼中有一個_section的變數實際是沒有定義的,而這個變數俺給它的含義是前邊所寫的『情境』,比如_section=='dev'表示開發環境,_section=='qa'表示QA環境,而咱們這次既然做的是一個類似exe的程式,所以_section需要在執行exe時候當作參數傳進來。

  這時候咱們就要用到Python的sys模組了,同樣import sys,然後在程式中通過:

  ?

1

_section = sys.argv[1] if len(sys.argv) > 1 else 'dev'

  這樣的方式來擷取『情境』這個參數,這一段代碼就會變成:

  ?

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

  既然已經讀取到設定檔中的ProxyServer和ProxyOverride這倆東東了,寫入到註冊表理論上就能完成咱們的修改IE代理配置的大業了:

  ?

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)

  前一句為啥是『理論上』呢,因為註冊表內容雖然已經修改了,但實際上IE瀏覽器並沒有生效,讓IE瀏覽器生效需要關閉重新開啟。

  這時候就用到前邊安裝的一個叫做WMI的東東,import wmi ctypes,然後:

  ?

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)

  當然,這段代碼是有一點點問題的,只關閉了IE沒有重新開啟……是因為俺偷懶了,俺可以接受手動開啟IE…

  綜上所述:

  完整的代碼為:

  ?

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

43

44

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

84

85

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.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('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.argv) > 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')

 

_ProxyOverride = config.get(_section, 'ProxyOverride')

 

changeIEProxy('ProxyServer', _ProxyServer)

 

changeIEProxy('ProxyOverride', _ProxyOverride)

 

print 'done, open ie'

 

else:

 

print 'config.ini is created, modify config.ini and try again'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.