主要用来修改本地hosts文件
1、将run.py文件放在C:\Users\Administrator目录下
2.打开cmd命令行。执行
python run.py add 127.0.01 www.baidu.com
python run.py del 127.0.01 www.baidu.com
# -*- coding: cp936 -*-
# 防止命令行显示中文乱码
import sys
import os
path = "文件路径"
def add(ip , domain): # 在文件下增加内容
try:
with open(path, 'a+') as text:
for line in text.readlines():
if ip + " "+ domain in line:
print("已经存在")
break
text.write("\n"+ ip + " "+ domain)
except:
print("添加出错")
def delete(ip , domain):
with open(path) as text, open('2.txt', "w") as new_text:
try:
for line in text.readlines():
new_line = line.replace("\n"+ip + " "+ domain, "")
new_text.write(new_line)
except:
print("删除出错")
os.remove(path)
os.rename("2.txt", "文件名")
if __name__ == '__main__':
command = sys.argv[1].lower() # 参数一
ip = sys.argv[2] # 参数二
domain = sys.argv[3] # 参数三
if command == "add":
add(ip, domain)
elif command == "del":
delete(ip, domain)
else:
print("没有此命令")