Created
March 2, 2026 06:01
-
-
Save zouzanyan/ae8d5c1c83b739825443c577f2097c1e to your computer and use it in GitHub Desktop.
切换git 代理python脚本
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| import subprocess | |
| import sys | |
| # 固定代理地址 | |
| PROXY = "http://127.0.0.1:10808" | |
| def run(cmd): | |
| result = subprocess.run( | |
| cmd, | |
| shell=True, | |
| stdout=subprocess.PIPE, | |
| stderr=subprocess.PIPE, | |
| text=True | |
| ) | |
| return result.stdout.strip(), result.stderr.strip(), result.returncode | |
| def set_proxy(): | |
| print(f"Enabling git proxy: {PROXY}") | |
| run(f"git config --global http.proxy {PROXY}") | |
| run(f"git config --global https.proxy {PROXY}") | |
| print("Done.") | |
| def unset_proxy(): | |
| print("Disabling git proxy") | |
| run("git config --global --unset http.proxy") | |
| run("git config --global --unset https.proxy") | |
| print("Done.") | |
| def show_proxy(): | |
| http, _, _ = run("git config --global --get http.proxy") | |
| https, _, _ = run("git config --global --get https.proxy") | |
| print("Current Git proxy:") | |
| print("HTTP :", http if http else "Not set") | |
| print("HTTPS :", https if https else "Not set") | |
| def usage(): | |
| print(""" | |
| Usage: | |
| python git_proxy_switch.py on | |
| python git_proxy_switch.py off | |
| python git_proxy_switch.py status | |
| """) | |
| def main(): | |
| if len(sys.argv) != 2: | |
| usage() | |
| return | |
| action = sys.argv[1].lower() | |
| if action == "on": | |
| set_proxy() | |
| elif action == "off": | |
| unset_proxy() | |
| elif action == "status": | |
| show_proxy() | |
| else: | |
| usage() | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment