-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUpdatePacFile.py
72 lines (60 loc) · 2.31 KB
/
UpdatePacFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import base64
import os
import requests
'''
@author ClouderDC
@date 20181210
'''
class GetGfwFile(object):
url="https://raw.githubusercontent.com/gfwlist/gfwlist/master/gfwlist.txt"
#检测网络连通性
def _CheckNetwork(self):
testVal=os.popen("ping 114.114.114.114 -c 1").readlines()
try:
packetsLoss=testVal[4].split()
if packetsLoss[5]=="0%":
returnVal="True"
return returnVal
except:
print("Network Error")
returnVal="False"
return returnVal
#解析gfwlist编码
def _Base64Analysis(self,url):
try:
res=requests.get(self.url)
resBase64=res.text
resValue=base64.b64decode(resBase64)
return str(resValue,encoding="utf-8")
except:
print("Analysis Error!")
#指定路径创建pac文件
def _WriteFile(self,filePath):
try:
getListFileValues=self._Base64Analysis(self.url)
if os.path.exists(filePath)==True:#如果存在,先删除再创建
os.remove(filePath)
with open(filePath,"a+") as f:
f.writelines(getListFileValues) #将解析结果写入指定文件
if(os.path.exists(filePath))==True:
print("Create success,plz check your create file: "+filePath)
else:
print("Create file failure")
else:
with open(filePath, "a+") as f:
f.writelines(getListFileValues)
if (os.path.exists(filePath)) == True:
print("Create success,plz check your create file: " + filePath)
else:
print("Create file failure")
except:
print("File Write Error!")
#执行
def run(self):
if self._CheckNetwork()!="False":
print("运行此脚本请保证脚本的运行权限,并保证能联通互联网的情况下;请给需要创建的文件指定绝对路径,如没有指定路径,便在本脚本所在目录下创建。望您知晓")
filePath = input("请输入文件路径:")
self._WriteFile(filePath)
if __name__ == '__main__':
getGfwFile=GetGfwFile()
getGfwFile.run()