dm/dm.py

183 lines
6.2 KiB
Python
Raw Normal View History

2023-09-10 00:15:03 +08:00
import socket
2023-08-31 00:51:35 +08:00
import sys
2023-09-10 00:15:03 +08:00
import pymysql
2023-08-31 00:51:35 +08:00
from PySide6 import QtWidgets
from PySide6.QtWidgets import QApplication, QMainWindow, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget, \
2023-09-10 00:15:03 +08:00
QTextEdit, QPushButton, QHBoxLayout, QLineEdit, QMessageBox
2023-08-31 00:51:35 +08:00
class TableWidgetExample(QMainWindow):
def __init__(self):
super().__init__()
2023-09-10 00:15:03 +08:00
self.connection = None
self.cursor = None
self.is_connect = False
# self.button_add = None
# self.button_execute = None
# self.input_text = None
# self.connection_host = None
# self.connection_port = None
# self.connection_user = None
# self.connection_pwd = None
2023-08-31 00:51:35 +08:00
self.initUI()
def initUI(self):
2023-09-10 00:15:03 +08:00
self.setWindowTitle("采集")
2023-08-31 00:51:35 +08:00
self.setGeometry(100, 100, 800, 600)
2023-08-31 18:18:18 +08:00
central_widget = QWidget()
center_layout = QVBoxLayout(central_widget)
# central_widget.setLayout(center_layout)
2023-08-31 00:51:35 +08:00
self.setCentralWidget(central_widget)
2023-08-31 18:18:18 +08:00
top_widget = QWidget()
top_layout = QVBoxLayout(top_widget)
table = QTableWidget()
2023-09-09 20:31:45 +08:00
table.setColumnCount(2)
2023-08-31 00:51:35 +08:00
2023-09-09 20:31:45 +08:00
table.setHorizontalHeaderLabels(["URL", "状态"])
2023-08-31 00:51:35 +08:00
header = table.horizontalHeader()
header.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
header.setSectionResizeMode(0, QtWidgets.QHeaderView.Stretch)
2023-09-10 17:13:33 +08:00
# data = [
# ["https://baidu.com", "队列中"],
# ["https://google.com", "队列中"],
# ["https://rainss.cn", "队列中"],
# ["https://baidu.com", "队列中"],
# ["https://google.com", "队列中"],
# ["https://rainss.cn", "队列中"],
# ["https://baidu.com", "队列中"],
# ["https://google.com", "队列中"],
# ["https://rainss.cn", "队列中"],
# ["https://baidu.com", "队列中"],
# ["https://google.com", "队列中"],
# ["https://rainss.cn", "队列中"]
# ]
#
# table.setRowCount(len(data))
#
# for row, rowData in enumerate(data):
# for col, value in enumerate(rowData):
# item = QTableWidgetItem(value)
# table.setItem(row, col, item)
2023-08-31 00:51:35 +08:00
top_layout.addWidget(table)
2023-08-31 18:18:18 +08:00
center_layout.addWidget(top_widget)
2023-08-31 00:51:35 +08:00
# 下部分布局
bottom_widget = QWidget()
2023-09-09 20:31:45 +08:00
bottom_widget.setFixedHeight(200)
2023-08-31 00:51:35 +08:00
bottom_layout = QVBoxLayout(bottom_widget)
2023-09-10 00:15:03 +08:00
self.input_text = QTextEdit()
self.input_text.setPlaceholderText("请输入链接,一行一个")
bottom_layout.addWidget(self.input_text)
connection_layout = QHBoxLayout()
self.connection_host = QLineEdit()
self.connection_host.setPlaceholderText("地址")
self.connection_port = QLineEdit()
self.connection_port.setPlaceholderText("端口")
2023-08-31 18:18:18 +08:00
2023-09-10 00:15:03 +08:00
self.connection_dbname = QLineEdit()
self.connection_dbname.setPlaceholderText("数据库")
2023-09-09 20:31:45 +08:00
2023-09-10 00:15:03 +08:00
self.connection_user = QLineEdit()
self.connection_user.setPlaceholderText("用户名")
self.connection_pwd = QLineEdit()
self.connection_pwd.setPlaceholderText("密码")
self.connection_pwd.setEchoMode(QLineEdit.Password)
self.button_connection = QPushButton("连接")
self.button_connection.clicked.connect(self.connect_to_database)
connection_layout.addWidget(self.connection_host)
connection_layout.addWidget(self.connection_port)
2023-09-10 17:13:33 +08:00
connection_layout.addWidget(self.connection_dbname)
2023-09-10 00:15:03 +08:00
connection_layout.addWidget(self.connection_user)
connection_layout.addWidget(self.connection_pwd)
connection_layout.addWidget(self.button_connection)
bottom_layout.addLayout(connection_layout)
action_layout = QHBoxLayout()
button_add = QPushButton("添加链接")
action_layout.addWidget(button_add)
button_execute = QPushButton("开始采集")
action_layout.addWidget(button_execute)
bottom_layout.addLayout(action_layout)
center_layout.addWidget(bottom_widget)
2023-09-09 20:31:45 +08:00
2023-09-10 00:15:03 +08:00
def connect_to_database(self):
# 数据库基本信息
host = self.connection_host.text()
port = self.connection_port.text()
user = self.connection_user.text()
pwd = self.connection_pwd.text()
dbname = self.connection_dbname.text()
2023-09-10 17:13:33 +08:00
if not host or not port or not dbname or not user or not pwd:
self.statusBar().showMessage("主机、端口、数据库名、用户名和密码不能为空!")
QMessageBox.critical(self, "Error", "主机、端口、数据库名、用户名和密码不能为空!")
2023-09-10 00:15:03 +08:00
return
try:
socket.gethostbyname(host)
except socket.error:
self.statusBar().showMessage("无效的主机!")
QMessageBox.critical(self, "Error", "无效的主机!")
return
else:
# 主机有效
print("有效的主机")
# 校验端口
try:
port = int(port)
if 0 < port <= 65535:
print("有效的端口")
else:
QMessageBox.critical(self, "Error", "端口超出范围!")
self.statusBar().showMessage("端口超出范围!")
return
except ValueError:
QMessageBox.critical(self, "Error", "无效的端口!")
self.statusBar().showMessage("无效的端口!")
return
try:
# 连接数据库
self.connection = pymysql.connect(host=host, port=port, user=user, password=pwd, db=dbname)
self.cursor = self.connection.cursor()
# 链接成功
self.is_connect = True
self.statusBar().showMessage("Connection succees!")
except pymysql.Error as e:
# 异常 连接失败
self.is_connect = False
QMessageBox.critical(self, "Error", str(e))
self.statusBar().showMessage("Connection failed")
def closeEvent(self, event):
if self.connection:
self.connection.close()
event.accept()
2023-09-09 20:31:45 +08:00
2023-08-31 00:51:35 +08:00
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TableWidgetExample()
window.show()
sys.exit(app.exec())