Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set http proxy ip in linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to set up http proxy ip in linux". The explanation in this article is simple and clear, easy to learn and understand. Please follow the editor's train of thought to study and learn "how to set http proxy ip in linux".

The specific implementation steps are as follows:

1. Install pywin32 and WMI support.

Google the specific download address, because mine is a 32-bit python2.7 series, and the downloaded file names are (pywin32-218.win32-py2.7.exe, WMI-1.4.7.win32.exe).

First of all, we know that the proxy content of the IE browser is stored in the registry "HKEYCURRENTUSER\ Software\ Microsoft\ Windows\ CurrentVersion\ Internet Settings", so we can theoretically switch IE agents by changing the relevant key values here.

2. Modify the registry key value.

So by 1, the first function is to modify the registry key:

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 pywin32 is used in the code, so at the beginning of the file, you need to do import win32api, win32con, and introduce the relevant class.

There are only a few lines of the function to modify the system registry. Of course, because our factory has to surf the Internet through a proxy server, I only use REG_SZ to modify the key type of the system registry. In other cases, there will be REG_DWORD and so on.

3. A configuration file is needed to save different configuration information for various scenarios.

Then we need a configuration file to save different configuration information for various scenarios "QA ah development environment". At this time, the configuration file I use is in ini format, which can be parsed with Python's own ConfigParser.

Instead of using the XML or json that I am most familiar with in the past, it is purely for the purpose of loading XJI XML and json that I always think it is used on web. Ini looks more like a .exe more commonly used configuration file format.

Also because I have never used a configuration file in ini format before, this time I should learn another way to play Python.

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

4. Import ConfigParser at the beginning of the document.

Again, because ConfigParser is used, you need to import ConfigParser it at the beginning of the file.

Careful friends will notice that there is a variable of _ section in this code that is actually undefined, and the meaning of this variable is the "scenario" written earlier, such as _ section=='dev' represents the development environment, _ section=='qa' represents the QA environment, and since we are doing a program similar to exe this time, _ section needs to be passed as a parameter when executing exe.

At this point, we are going to use Python's sys module, the same import sys, and then pass through the program:

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

To get the "scene" parameter in this way, 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 we have read the ProxyServer and ProxyOverride in the configuration file, writing to the registry can theoretically complete our task of modifying the IE agent 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)

Because the registry content has been modified, but in fact, the IE browser does not take effect, for the IE browser to take effect needs to be closed and reopened.

At this point, use the front to install 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, there is something wrong with this code, which only closes the IE and does not reopen it.

To 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 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 () # Development Environment cfg.add_section (' dev') cfg.set ('dev',' ProxyServer', '192.168.0.6 virtual 3128') cfg.set ('dev',' ProxyOverride' 'localhost 127.0.0.1') # pre-launch cfg.add_section ('prepare') cfg.set (' prepare', 'ProxyServer',' 192.168.0.6 cfg.set 3128') cfg.set ('prepare',' ProxyOverride', 'localhost;127.0.0.1 ') # online cfg.add_section (' online') cfg.set ('online',' ProxyServer', '192.168.0.6 cfg.set 3128') cfg.set ('online',' ProxyOverride', 'localhost 127.0.0.1') # QA cfg.add_section ('qa') cfg.set (' qa', 'ProxyServer',' 192.168.2.16 cfg.set 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' Thank you for reading The above is the content of "how to set http proxy ip in linux". After the study of this article, I believe you have a deeper understanding of how to set http proxy ip in linux, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development