#coding:gbk
importosimportsysfrom subprocess import *
importtimedefDenyfile():if not os.path.exists('D:\DenyHttp'):
os.mkdir('D:\DenyHttp')
os.mkdir(r'D:\DenyHttp\restore')
hostsName= 'C:\Windows\System32\drivers\etc\hosts'filename= r'D:\DenyHttp\hosts'
'''若存在原文件,删除'''
if os.path.exists(r'D:\DenyHttp\hosts') and os.path.exists(r'D:\DenyHttp\restore\hosts'):print(filename + '已存在!开始删除文件')
os.system("del /f /s /q D:\DenyHttp\*.*")
os.system("rd /s /q D:\DenyHttp")print('文件已经删除!')'''删除后创建新的目录'''
print('创建文件夹:')
os.system('mkdir d:\\DenyHttp\\restore')print('准备复制:')if not os.system(r'copy'+ hostsName +'D:\DenyHttp'):print('file remove from' + hostsName + 'to' +filename)if not os.system(r'copy'+ hostsName + r'D:\DenyHttp\restore'):print('file remove from' + hostsName + 'to D:\DenyHttp\restore')
L=[]
denyNetAddress=getHTTPList(L)'''添加htttp到文件'''
for s indenyNetAddress:
os.system('echo' + s +'>>' +filename)'''修改hosts文件'''
if not os.system(r'copy D:\DenyHttp\hosts C:\Windows\System32\drivers\etc'):print('file hosts has been already discovered!')defRecoverfile():if not os.system(r'copy D:\DenyHttp\restore\hosts C:\Windows\System32\drivers\etc'):print('file hosts has been already recovered!')defdetectnet():
L=[]
denyNetAddress=getNetList(L)
mark= 1
for s indenyNetAddress:
p= Popen(["ping.exe", s],
stdin=PIPE,stdout=PIPE,stderr=PIPE,
shell=True)
out=p.stdout.read()if '127.0.0.1' instr(out):
mark=0'''mark为假不能访问'''
if notmark:print('can\'t request http')else:print('can request http')returnmarkdef getHTTPList(denyNetAddress =[]):
denyNetAddress.append('127.0.0.1 sg.search.yahoo.com')
denyNetAddress.append('127.0.0.1 www.cnblogs.com')
denyNetAddress.append('127.0.0.1 www.baidu.com')
denyNetAddress.append('127.0.0.1 www.sogou.com')
denyNetAddress.append('127.0.0.1 blog.csdn.net')
denyNetAddress.append('127.0.0.1 www.soso.com')
denyNetAddress.append('127.0.0.1 cn.bing.com')'''1.0修复'''denyNetAddress.append('127.0.0.1 www.so.com')returndenyNetAddressdef getNetList(denyNetAddress =[]):
denyNetAddress.append('www.baidu.com')
denyNetAddress.append('www.sogou.com')
denyNetAddress.append('www.soso.com')
denyNetAddress.append('cn.bing.com')
denyNetAddress.append('www.so.com')
denyNetAddress.append('sg.search.yahoo.com')
denyNetAddress.append('www.cnblogs.com')
denyNetAddress.append('blog.csdn.net')returndenyNetAddressif __name__ == '__main__':#Denyfile()
Recoverfile()#print(detectnet())