1
+ import subprocess
2
+ import argparse
3
+
4
+ def set_proxy (proxy_host , proxy_port ):
5
+ """Set HTTP and HTTPS proxy settings using gsettings."""
6
+ try :
7
+ # Set proxy mode to manual
8
+ subprocess .run (
9
+ ["gsettings" , "set" , "org.gnome.system.proxy" , "mode" , "manual" ],
10
+ check = True
11
+ )
12
+ # Set HTTP proxy
13
+ subprocess .run (
14
+ ["gsettings" , "set" , "org.gnome.system.proxy.http" , "host" , proxy_host ],
15
+ check = True
16
+ )
17
+ subprocess .run (
18
+ ["gsettings" , "set" , "org.gnome.system.proxy.http" , "port" , str (proxy_port )],
19
+ check = True
20
+ )
21
+ # Set HTTPS proxy
22
+ subprocess .run (
23
+ ["gsettings" , "set" , "org.gnome.system.proxy.https" , "host" , proxy_host ],
24
+ check = True
25
+ )
26
+ subprocess .run (
27
+ ["gsettings" , "set" , "org.gnome.system.proxy.https" , "port" , str (proxy_port )],
28
+ check = True
29
+ )
30
+ # Optionally, set no_proxy to bypass localhost
31
+ subprocess .run (
32
+ ["gsettings" , "set" , "org.gnome.system.proxy" , "ignore-hosts" , "['localhost', '127.0.0.1', '::1']" ],
33
+ check = True
34
+ )
35
+ print (f"Successfully set HTTP and HTTPS proxies to { proxy_host } :{ proxy_port } " )
36
+ except subprocess .CalledProcessError as e :
37
+ print (f"Error setting proxy: { e } " )
38
+
39
+ def unset_proxy ():
40
+ """Unset proxy settings by switching to 'none' mode."""
41
+ try :
42
+ subprocess .run (
43
+ ["gsettings" , "set" , "org.gnome.system.proxy" , "mode" , "none" ],
44
+ check = True
45
+ )
46
+ print ("Successfully unset HTTP and HTTPS proxies" )
47
+ except subprocess .CalledProcessError as e :
48
+ print (f"Error unsetting proxy: { e } " )
49
+
50
+ def main ():
51
+ parser = argparse .ArgumentParser (description = "Manage HTTP and HTTPS proxy settings on Ubuntu" )
52
+ parser .add_argument (
53
+ "action" ,
54
+ choices = ["set" , "unset" ],
55
+ help = "Action to perform: set or unset proxy"
56
+ )
57
+ args = parser .parse_args ()
58
+
59
+ # Default proxy settings (modify as needed)
60
+ proxy_host = "127.0.0.1"
61
+ proxy_port = 7890
62
+
63
+ if args .action == "set" :
64
+ set_proxy (proxy_host , proxy_port )
65
+ else :
66
+ unset_proxy ()
67
+
68
+ if __name__ == "__main__" :
69
+ main ()
0 commit comments