PyQt5,切换host小工具完整代码(界面与代码分离版)

PyQt5,切换host小工具完整代码(界面与代码分离版)

工作中,需要来回切换环境,windows电脑需要到C盘的原文件下面修改,很麻烦,所有写了一个小工具来切换host

from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1005, 768)
        self.testHost = QtWidgets.QRadioButton(Form)
        self.testHost.setGeometry(QtCore.QRect(170, 240, 151, 31))
        self.testHost.setObjectName("testHost")
        self.label_5 = QtWidgets.QLabel(Form)
        self.label_5.setGeometry(QtCore.QRect(380, 80, 401, 31))
        font = QtGui.QFont()
        font.setPointSize(14)
        font.setBold(True)
        font.setWeight(75)
        self.label_5.setFont(font)
        self.label_5.setObjectName("label_5")
        self.lineEdit_3 = QtWidgets.QLineEdit(Form)
        self.lineEdit_3.setEnabled(False)
        self.lineEdit_3.setGeometry(QtCore.QRect(340, 370, 471, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineEdit_3.setFont(font)
        self.lineEdit_3.setObjectName("lineEdit_3")
        self.GrayscaleHost = QtWidgets.QRadioButton(Form)
        self.GrayscaleHost.setGeometry(QtCore.QRect(170, 300, 151, 31))
        self.GrayscaleHost.setObjectName("GrayscaleHost")
        self.settingHost = QtWidgets.QPushButton(Form)
        self.settingHost.setGeometry(QtCore.QRect(360, 480, 261, 41))
        self.settingHost.setCheckable(False)
        self.settingHost.setDefault(False)
        self.settingHost.setObjectName("settingHost")
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setEnabled(False)
        self.lineEdit.setGeometry(QtCore.QRect(340, 240, 471, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineEdit.setFont(font)
        self.lineEdit.setInputMask("")
        self.lineEdit.setPlaceholderText("")
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(Form)
        self.lineEdit_2.setEnabled(False)
        self.lineEdit_2.setGeometry(QtCore.QRect(340, 300, 471, 31))
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lineEdit_2.setFont(font)
        self.lineEdit_2.setInputMask("")
        self.lineEdit_2.setReadOnly(False)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.label_4 = QtWidgets.QLabel(Form)
        self.label_4.setGeometry(QtCore.QRect(190, 150, 421, 41))
        font = QtGui.QFont()
        font.setPointSize(11)
        font.setBold(True)
        font.setWeight(75)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.FormalHost = QtWidgets.QRadioButton(Form)
        self.FormalHost.setGeometry(QtCore.QRect(170, 370, 151, 31))
        self.FormalHost.setObjectName("FormalHost")
        self.closeChrome = QtWidgets.QPushButton(Form)
        self.closeChrome.setGeometry(QtCore.QRect(690, 150, 101, 41))
        self.closeChrome.setObjectName("closeChrome")
        self.refresh = QtWidgets.QPushButton(Form)
        self.refresh.setGeometry(QtCore.QRect(720, 460, 91, 61))
        self.refresh.setObjectName("refresh")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.testHost.setText(_translate("Form", "测试环境host"))
        self.label_5.setText(_translate("Form", "host快捷配置工具"))
        self.lineEdit_3.setText(_translate("Form", "  xxx.xxx.xx.227 xxx.xxsxx.net #正式环境host"))
        self.GrayscaleHost.setText(_translate("Form", "灰度环境host"))
        self.settingHost.setText(_translate("Form", "一键设置host并打开浏览器"))
        self.lineEdit.setText(_translate("Form", "  xxx.xxx.xx.229 xxx.xxsxx.net #测试环境host "))
        self.lineEdit_2.setText(_translate("Form", "  xxx.xxx.xx.224 xxx.xxsxx.net #灰度环境host"))
        self.label_4.setText(_translate("Form", "设置host前,请点击按钮关闭所有浏览器"))
        self.FormalHost.setText(_translate("Form", "正式环境host"))
        self.closeChrome.setText(_translate("Form", "关闭浏览器"))
        self.refresh.setText(_translate("Form", "清除缓存"))


import sys, os, subprocess
from PyQt5.QtWidgets import QApplication, QMainWindow,QMessageBox
from PyQt5 import QtWidgets
import onehost
import time
host = "C:\Windows\System32\drivers\etc\hosts"
start1 = "start chrome.exe https://xxxx1.com/passport"
start2 = "start chrome.exe https://xxxx2.com/passport"
start3 = "start chrome.exe https:/xxxx3.com/passport"

class MainCode(QMainWindow, onehost.Ui_Form):
    def __init__(self):
        QMainWindow.__init__(self)
        onehost.Ui_Form.__init__(self)
        self.uiForm = onehost.Ui_Form()
        self.uiForm.setupUi(self)
        self.uiForm.closeChrome.clicked.connect(self.close_Chrome)
        self.uiForm.settingHost.clicked.connect(self.setting_Host)
        self.uiForm.refresh.clicked.connect(self.Refresh)

        # self.uiForm.testHost.clicked.connect(self.testhost)
    '''
    关闭浏览器
    '''

    def close_Chrome(self):
        # 调用外部程序
        print("连接成功")
        browserName = "chrome.exe"
        cmd = "taskkill /f /t /im {}".format(browserName)
        os_cmd = subprocess.Popen(cmd, shell=True, encoding="utf-8")

        QMessageBox.information(self, '关闭提醒', '您已成功关闭chrome')
    '''
        设置host
    '''

    def setting_Host(self):
        def writehost(host, content):
            f = open(host, "w", encoding='utf-8')
            f.write(content)
            f.close()
            time.sleep(1)

        if self.uiForm.testHost.isChecked() is True:
            contest = self.uiForm.lineEdit.text()
            writehost(host, contest)
            QtWidgets.QMessageBox.information(self, '设置提醒', '设置测试host环境成功')

            os_cmd = subprocess.Popen(start1, shell=True, encoding="utf-8")
        elif self.uiForm.GrayscaleHost.isChecked() is True:
            contest = self.uiForm.lineEdit_2.text()
            writehost(host, contest)
            QtWidgets.QMessageBox.information(self, '设置提醒', '设置灰度环境host成功')
            os_cmd = subprocess.Popen(start2, shell=True, encoding="utf-8")
        elif self.uiForm.FormalHost.isChecked() is True:
            contest = self.uiForm.lineEdit_3.text()
            writehost(host, contest)
            QtWidgets.QMessageBox.information(self, '设置提醒', '设置正式host成功')
            os_cmd = subprocess.Popen(start3, shell=True, encoding="utf-8")
        else:
            QtWidgets.QMessageBox.information(self, '设置提醒', '请选择host')

    """
        刷新DNS缓存
    """
    def Refresh(self):
        try:
            f = open(host, "w", encoding='utf-8')
            code = "ipconfig /flushdns"
            os_cmd = subprocess.Popen(code, shell=True, encoding="utf-8")
            time.sleep(1)
            QtWidgets.QMessageBox.information(self, '设置提醒', '刷新DNS成功')
        except:
            QtWidgets.QMessageBox.information(self, '设置提醒', '未刷新成功,请重试')


if __name__ == '__main__':
    app = QApplication(sys.argv)
    MainWindow = MainCode()
    MainWindow.show()
    sys.exit(app.exec_())

以下是界面展示
在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值