In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to write Python code to set up the agent configuration under each platform? in view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
What we bring today is the way to set up the socks5 agent configuration for each platform with Python code.
As the articles set up on various platforms on the Internet are relatively scattered, and there are some holes in individual platform settings, Brother Ma decided to write a summary article for others to refer to.
Disclaimer: this article does not explain the implementation of the socks5 proxy service, but only shows how to use code to set up socks5 configuration under Windows, Linux, and OSX.
Sometimes, some desktop programs may need to use the agent configuration function, such as some tool software within the enterprise to access the company's internal resources, at this time, the traffic of the software needs to be transferred to the company's designated internal service.
Obviously, when we use the desktop system on a daily basis, we can set up the agent configuration manually. However, if a piece of software still needs to be set up by users by hand, it will make it more difficult for users to learn and reduce the user experience of the software, so developers will need to know how to modify the settings with code.
This paper only takes Python as an example to explain. Because of the implementation characteristics of Python library, its library function interface is almost the same as the C version interface prototype, so it is also helpful for developers to use it for reference.
Let's give examples platform by platform. Because there are many ways to set up an agent, Brother Ma has not tried it one by one, so he only gives the feasible scheme that he has tried:
OSX (Mac) import os# opens agent os.popen ('networksetup-setsocksfirewallproxy "Wi-Fi" SOCKS5_PROXY_IP SOCKS5_PROXY_PORT'). Close () # closes agent os.popen (' networksetup-setsocksfirewallproxystate "Wi-Fi" off'). Close ()
Where SOCKS5_PROXY_IP is the IP address of the agent and SOCKS5_PROXY_PORT is the port of the agent.
Mac is actually set up from the command line, and there is a tool in OSX called networksetup that can be used to set up proxies.
Linuximport os# opens the agent os.popen ('gsettings set org.gnome.system.proxy mode "manual"). Close () os.popen (' gsettings set org.gnome.system.proxy.socks host "SOCKS5_PROXY_IP"'). Close () os.popen ('gsettings set org.gnome.system.proxy.socks port SOCKS5_PROXY_PORT'.format (conf.LISTENPORT)). Close () os.popen (' gsettings set org.gnome.system.proxy ignore-hosts "IGNORED_IPs"'. Format (ignore). Close () # close the agent os.popen ('gsettings set org.gnome.system.proxy mode "none"'). Close ()
Among them, SOCKS5_PROXY_IP is the same as SOCKS5_PROXY_PORT and OSX. IGNORED_IPs is an IP list that does not leave the agent, and its format is as follows:
['localhost',' 127.0.0.0amp 8pm,':: 1']
Notice that what is written here is not a Python array, but a string. We can add the IP that does not leave the agent in [] and enclose it in quotation marks to separate it from other addresses with commas.
Windows#encoding=utf8from ctypes import * from ctypes.wintypes import * import winregimport settings as confLPWSTR = POINTER (WCHAR) HINTERNET = LPVOIDINTERNET_PER_CONN_PROXY_SERVER = 2INTERNET_OPTION_REFRESH = 37INTERNET_OPTION_SETTINGS_CHANGED = 39INTERNET_OPTION_PER_CONNECTION_OPTION = 3INTERNET_PER_CONN_FLAGS = 1class INTERNET_PER_CONN_OPTION (Structure): class Value (Union): _ fields_ = [('dwValue', DWORD), (' pszValue') LPWSTR), ('ftValue', FILETIME),] _ fields_ = [(' dwOption', DWORD), ('Value', Value),] class INTERNET_PER_CONN_OPTION_LIST (Structure): _ fields_ = [(' dwSize', DWORD), ('pszConnection', LPWSTR), (' dwOptionCount', DWORD), ('dwOptionError', DWORD) ('pOptions', POINTER (INTERNET_PER_CONN_OPTION)),] def set_proxy_settings (serverIp, status='off'): whitelist = "IGNORED_IPs" if status= =' on': setting = create_unicode_buffer ("SOCKS5_PROXY_IP:SOCKS5_PROXY_PORT") whitelist + =' {} {} '.format (serverIp, conf.WEBHOST) else: setting = None InternetSetOption = windll.wininet.InternetSetOptionW InternetSetOption.argtypes = [HINTERNET, DWORD, LPVOID DWORD] InternetSetOption.restype = BOOL List = INTERNET_PER_CONN_OPTION_LIST () Option = (INTERNET_PER_CONN_OPTION * 3) () nSize = c_ulong (sizeof (INTERNET_PER_CONN_OPTION_LIST)) Option [0] .dwOption = INTERNET_PER_CONN_FLAGS Option [0] .Value.dwValue = (2 if status=='on' else 1) Option [1] .dwOption = INTERNET_PER_CONN_PROXY_SERVER Option [1] .Value.pszValue = setting Option [2] .dwOption = INTERNET_PER_CONN_PROXY_BYPASS Option [2] .Value.pszValue = create_unicode_buffer (whitelist) List.dwSize = sizeof (INTERNET_PER_CONN_OPTION_LIST) List.pszConnection = None List.dwOptionCount = 3 List.dwOptionError = 0 List.pOptions = Option InternetSetOption (None INTERNET_OPTION_PER_CONNECTION_OPTION, byref (List), nSize) if status = 'on': key = winreg.OpenKey (winreg.HKEY_CURRENT_USER, r'Software\ Microsoft\ Windows\ CurrentVersion\ Internet Settings', 0, winreg.KEY_WRITE) winreg.SetValueEx (key,' ProxyServer', 0,1, 'socks://SOCKS5_PROXY_IP:SOCKS5_PROXY_PORT') winreg.CloseKey (key) InternetSetOption (None, INTERNET_OPTION_SETTINGS_CHANGED, None 0) InternetSetOption (None, INTERNET_OPTION_REFRESH, None, 0) if _ _ name__ = = "_ _ main__": # Open agent set_proxy_settings ('127.0.0.1,' on') # close agent set_proxy_settings ('')
As you can see, the setup of windows is much more complex. Where IGNORED_IPs is a list of IP addresses that do not follow the proxy, which is different from Linux, as shown below:
Localhost;127.*;10.*;172.16.*;172.17.*;172.18.*;172.19.*;172.20.*;172.21.*;172.22.*;172.23.*;172.24.*;172.25.*;172.26.*;172.27.*;172.28.*;172.29.*;172.30.*;172.31.*;172.32.*;192.168.*
That is, addresses are separated by semicolons and are not enclosed in parentheses.
Although we can also use the command line to modify the registry to achieve this, but the program packaged in pyinstaller will be directly killed by the system firewall, and even if it can run, the agent configuration modification takes at least 10 minutes to take effect, the experience is very poor.
At the same time, one of the drawbacks of Windows is that when you manually set up the socks proxy, you see that the value of the registry key ProxyServer is socks = SOCKS5_PROXY_IP:SOCKS5_PROXY_PORT, but if this is really set, then the agent must receive a socks4 message, not a socks5.
This is the answer to the question about how to write Python code to set up the agent configuration under each platform. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.