Setting up SOCKS Agent
In macOS systems, the SOCKS proxy can be set up through the networksetup tool. Here are the methods implemented in Python:
Set up SOCKS proxy using networksetup
import subprocess def set_socks_proxy(server, port): """ Set up the SOCKS proxy for macOS system. :param server: proxy server address :param port: proxy port """ try: # Set up SOCKS proxy ( ['networksetup', '-setsocksfirewallproxy', 'Wi-Fi', server, str(port)], check=True ) print(f"SOCKS The proxy has been set to {server}:{port}") except as e: print(f"set up SOCKS Agent failed: {e}") def disable_socks_proxy(): """ Disable the SOCKS proxy for macOS systems. """ try: # Close the SOCKS proxy ( ['networksetup', '-setsocksfirewallproxystate', 'Wi-Fi', 'off'], check=True ) print("SOCKS Agent Disabled") except as e: print(f"Disabled SOCKS Agent failed: {e}") if __name__ == "__main__": # Set up SOCKS proxy set_socks_proxy('127.0.0.1', 1080) # Disable SOCKS proxy # disable_socks_proxy()
Parameter description
networksetup is a command line tool used by macOS to manage network settings.
-setsocksfirewallproxy is used to configure the SOCKS proxy.
-setsocksfirewallproxystate is used to enable or disable the SOCKS proxy.
Wi-Fi is the name of the network service and may need to be adjusted according to actual conditions (such as Ethernet).
Check the network service name
Before executing the script, you can view the available network service names through the following command:
networksetup -listallnetworkservices
Find the correct service name (such as Wi-Fi or Ethernet) and replace it in the script.
Result verification
You can verify that the proxy is successfully set up by:
networksetup -getsocksfirewallproxy Wi-Fi
The output is similar to the following:
Enabled: Yes
Server: 127.0.0.1
Port: 1080
Authenticated Proxy Enabled: 0
Things to note
Permissions: Some network settings may require administrator permissions. You may need to use sudo when running scripts.
Automation tools: Ensure Python version supports it and run in a macOS environment.
Cleanup: After the script is completed, be sure to call disable_socks_proxy() to clear the settings if the proxy settings are no longer required.
Set up HTTP and HTTPS proxy
import subprocess def set_web_proxy(server, port): """ Set up the macOS system proxy. :param server: proxy server address :param port: proxy port """ # Set up HTTP and HTTPS proxy (['networksetup', '-setwebproxy', 'Wi-Fi', server, str(port)], check=True) (['networksetup', '-setsecurewebproxy', 'Wi-Fi', server, str(port)], check=True) print(f"HTTP and HTTPS The proxy is set to {server}:{port}") def disable_web_proxy(): """ Disable macOS system proxy. """ # Turn off HTTP and HTTPS proxy (['networksetup', '-setwebproxystate', 'Wi-Fi', 'off'], check=True) (['networksetup', '-setsecurewebproxystate', 'Wi-Fi', 'off'], check=True) print("HTTP and HTTPS proxy disabled") if __name__ == "__main__": # Set up a proxy set_proxy('127.0.0.1', 1081) # Disable proxy # disable_web_proxy()
This is the article about how Python implements the settings of macOS system agents. For more related content on Python macOS system agents, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!