界面比较简单:
代码如下:
import requests
import re
import os
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
class Stats:
def __init__(self):
qfle_stats=QFile('github.ui')
qfle_stats.open(QFile.ReadOnly)
qfle_stats.close()
self.ui=QUiLoader().load(qfle_stats)
self.ui.pushButton.clicked.connect(self.run)
def run(self):
self.fangwen1()
self.fangwen2()
self.fangwen3()
self.fangwen4()
def fangwen1(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
url = requests.get('https://github.com.ipaddress.com/',headers=headers).text
aa = re.findall("https://www.ipaddress.com/ipv4/" + '(.*?)' + r'\\', url)[0]
global a
a=aa+' '
self.ui.lineEdit.setText(aa)
def fangwen2(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
url = requests.get('https://fastly.net.ipaddress.com/github.global.ssl.fastly.net',headers=headers).text
aa = re.findall("https://www.ipaddress.com/ipv4/" + '(.*?)' + r'\\', url)[0]
print(aa)
global b
b=aa+' '
self.ui.lineEdit_2.setText(aa)
def fangwen3(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
url = requests.get('https://github.com.ipaddress.com/assets-cdn.github.com',headers=headers).text
aa = re.findall("https://www.ipaddress.com/ipv4/" + '(.*?)' + r'\\', url)
global c, d, e, f
c=aa[0]+' '
d=aa[1]+' '
e=aa[2]+' '
f=aa[3]+' '
self.ui.lineEdit_3.setText(aa[0])
self.ui.lineEdit_4.setText(aa[1])
self.ui.lineEdit_5.setText(aa[2])
self.ui.lineEdit_6.setText(aa[3])
def fangwen4(self):
with open('C:\Windows\System32\drivers\etc\hosts','r') as zz:
lines=zz.readlines()
self.remove()
global a,b,c,d,e,f
for line in lines:
biao=[a,b,c,d,e,f]
global x
try:
abc=re.findall('gith'+'(.*?)'+'\.',line)[0]
line_new=re.sub(r'((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}', biao[x], line)
print(line_new)
self.write(line_new)
x+=1
except IndexError:
print('捕获异常')
self.cmd()
def remove(self):
os.remove(r'C:\Windows\System32\drivers\etc\hosts')
def write(self,text):
with open('C:\Windows\System32\drivers\etc' + '/' + 'hosts', 'a+') as zz:
zz.write(text)
print('写入成功')
def cmd(self):
dama='ipconfig /flushdns'
d=os.system(dama)
print(d)
self.ui.label_4.setText('hosts更新成功')
if __name__ == '__main__':
x=0
a=''
b=''
c=''
d=''
e=''
f=''
app = QApplication([])
Stats = Stats()
Stats.ui.show()
app.exec_()
注:使用时退出安全卫生,不然可能hosts更新不成功、有空再优化一下代码。
|