From 1f49ce2ab5bb688223e31cb8bd31bdd9b4ced5e2 Mon Sep 17 00:00:00 2001 From: JRNitre Date: Sat, 15 Jun 2024 00:04:43 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=BD=E5=8A=A0=20Python=20=E9=A1=B9?= =?UTF-8?q?=E7=9B=AE=20-=20Project=2002=20-=20Project=2003=20-=20Project?= =?UTF-8?q?=2004=20-=20Project=2005=20-=20Project=2006?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 02_vscode_python_objectAi_20240425/main.py | 1 + .../.idea/.gitignore | 3 + .../03_pycharm_python_coursework_20240427.iml | 10 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .../.idea/misc.xml | 4 + .../.idea/modules.xml | 8 + .../.idea/vcs.xml | 6 + .../.idea/.gitignore | 3 + .../.idea/Python_pyqt_MainLearn.iml | 10 ++ .../.idea/inspectionProfiles/profiles_settings.xml | 6 + .../.idea/misc.xml | 4 + .../.idea/modules.xml | 8 + .../main.py | 162 ++++++++++++++++++ 05_pycharm_python_pyqt5学习/.idea/.gitignore | 3 + .../.idea/002_Python_pyqt5_学习.iml | 10 ++ .../inspectionProfiles/Project_Default.xml | 12 ++ .../inspectionProfiles/profiles_settings.xml | 6 + 05_pycharm_python_pyqt5学习/.idea/misc.xml | 4 + 05_pycharm_python_pyqt5学习/.idea/modules.xml | 8 + 05_pycharm_python_pyqt5学习/UserWindow.ui | 106 ++++++++++++ 05_pycharm_python_pyqt5学习/main.py | 94 ++++++++++ .../.idea/.gitignore | 3 + .../.idea/004_Python_Requist.iml | 8 + .../.idea/inspectionProfiles/Project_Default.xml | 56 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 + .../.idea/misc.xml | 4 + .../.idea/modules.xml | 8 + 06_pycharm_python_阿里云Api_DDNS/main.py | 152 ++++++++++++++++ 28 files changed, 711 insertions(+) create mode 100644 02_vscode_python_objectAi_20240425/main.py create mode 100644 03_pycharm_python_coursework_20240427/.idea/.gitignore create mode 100644 03_pycharm_python_coursework_20240427/.idea/03_pycharm_python_coursework_20240427.iml create mode 100644 03_pycharm_python_coursework_20240427/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 03_pycharm_python_coursework_20240427/.idea/misc.xml create mode 100644 03_pycharm_python_coursework_20240427/.idea/modules.xml create mode 100644 03_pycharm_python_coursework_20240427/.idea/vcs.xml create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/.gitignore create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/Python_pyqt_MainLearn.iml create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/misc.xml create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/modules.xml create mode 100644 04_pycharm_python_pyqt5_信号与槽综合Demo/main.py create mode 100644 05_pycharm_python_pyqt5学习/.idea/.gitignore create mode 100644 05_pycharm_python_pyqt5学习/.idea/002_Python_pyqt5_学习.iml create mode 100644 05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/Project_Default.xml create mode 100644 05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 05_pycharm_python_pyqt5学习/.idea/misc.xml create mode 100644 05_pycharm_python_pyqt5学习/.idea/modules.xml create mode 100644 05_pycharm_python_pyqt5学习/UserWindow.ui create mode 100644 05_pycharm_python_pyqt5学习/main.py create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/.gitignore create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/004_Python_Requist.iml create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/Project_Default.xml create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/misc.xml create mode 100644 06_pycharm_python_阿里云Api_DDNS/.idea/modules.xml create mode 100644 06_pycharm_python_阿里云Api_DDNS/main.py diff --git a/02_vscode_python_objectAi_20240425/main.py b/02_vscode_python_objectAi_20240425/main.py new file mode 100644 index 0000000..66632ea --- /dev/null +++ b/02_vscode_python_objectAi_20240425/main.py @@ -0,0 +1 @@ +print("test") \ No newline at end of file diff --git a/03_pycharm_python_coursework_20240427/.idea/.gitignore b/03_pycharm_python_coursework_20240427/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/03_pycharm_python_coursework_20240427/.idea/03_pycharm_python_coursework_20240427.iml b/03_pycharm_python_coursework_20240427/.idea/03_pycharm_python_coursework_20240427.iml new file mode 100644 index 0000000..74d515a --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/03_pycharm_python_coursework_20240427.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/03_pycharm_python_coursework_20240427/.idea/inspectionProfiles/profiles_settings.xml b/03_pycharm_python_coursework_20240427/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/03_pycharm_python_coursework_20240427/.idea/misc.xml b/03_pycharm_python_coursework_20240427/.idea/misc.xml new file mode 100644 index 0000000..e88bc27 --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/03_pycharm_python_coursework_20240427/.idea/modules.xml b/03_pycharm_python_coursework_20240427/.idea/modules.xml new file mode 100644 index 0000000..22081cc --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/03_pycharm_python_coursework_20240427/.idea/vcs.xml b/03_pycharm_python_coursework_20240427/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/03_pycharm_python_coursework_20240427/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/.gitignore b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/Python_pyqt_MainLearn.iml b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/Python_pyqt_MainLearn.iml new file mode 100644 index 0000000..62c640a --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/Python_pyqt_MainLearn.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/inspectionProfiles/profiles_settings.xml b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/misc.xml b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/misc.xml new file mode 100644 index 0000000..9004013 --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/modules.xml b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/modules.xml new file mode 100644 index 0000000..62fcd19 --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/04_pycharm_python_pyqt5_信号与槽综合Demo/main.py b/04_pycharm_python_pyqt5_信号与槽综合Demo/main.py new file mode 100644 index 0000000..8b5765d --- /dev/null +++ b/04_pycharm_python_pyqt5_信号与槽综合Demo/main.py @@ -0,0 +1,162 @@ +import sys +import time + +from PyQt5.QtWidgets import * +from PyQt5.QtCore import * + + +class MyWindow(QWidget): + + def __init__(self): + super().__init__() + self.setWindowTitle("信号与槽") + self.init_ui() + + # 主初始化方法 + def main_window_init(self): + # 设置窗口大小 + self.resize(300, 200) + # 主界面布局 [垂直布局] + self.main_layout = QVBoxLayout() + + def init_ui(self): + self.main_window_init() + + # 信息Box初始化 + self.message_widget_init() + # 设置Box初始化 + self.seeting_widget_init() + + # 将两个Group添加至主布局器中 + self.main_layout.addWidget(self.message_group_box) + self.main_layout.addWidget(self.seeting_group_box) + self.setLayout(self.main_layout) + + # 信息部分初始化方法 + def message_widget_init(self): + # 信息界面组Group + self.message_group_box = QGroupBox("信息") + + # 两个控件水平布局 + horizon_layout = QHBoxLayout() + + # 控件1:设置转盘控件 + self.control_1 = QDial() + # 是否循环滚动 [默认是] + self.control_1.setWrapping(True) + # 是否显示刻度 + self.control_1.setNotchesVisible(True) + # 绑定信号至槽函数Dialg_Signal + self.control_1.valueChanged.connect(self.Dialg_Signal) + + # 控件2:信息框 + # 信息框垂直布局 + message_vertical_layout = QVBoxLayout() + # 文字 + Message_Lable = QLabel("数值") + # 信息框 + self.control_2 = QSpinBox() + # 两个控件添加至垂直布局中 + message_vertical_layout.addWidget(self.control_2) + message_vertical_layout.addWidget(Message_Lable) + + # 两个控件绑定至垂直布局中 + horizon_layout.addWidget(self.control_1) + horizon_layout.addLayout(message_vertical_layout) + + # 将垂直布局添加至组中 + self.message_group_box.setLayout(horizon_layout) + + # 设置部分初始化方法 + def seeting_widget_init(self): + # 设置界面组Group + self.seeting_group_box = QGroupBox("设置") + # 控件网格布局 + self.grid_layout = QGridLayout() + + # 设置项:是否显示刻度 + self.seeting_norchesvisible() + self.seeting_notchesVisile() + + # 显示刻度组添加至网格布局中 + self.grid_layout.addWidget(self.norchesvisible_group_box,1,1) + # 是否循环组添加至网格布局中 + self.grid_layout.addWidget(self.notchesVisile_group_box,1,2) + + # 将网格布局添加至主Group中 + self.seeting_group_box.setLayout(self.grid_layout) + + # 设置项:是否显示刻度 + def seeting_norchesvisible(self): + # 显示刻度组Group + self.norchesvisible_group_box = QGroupBox("显示刻度") + + # 下拉选框控件[是否显示刻度] + self.Combo_NotchesVisible = QComboBox() + # 该Group中为垂直布局 + norchesvisible_vertical_layout = QVBoxLayout() + # 配置下拉选框是否可以编辑 + self.Combo_NotchesVisible.setEditable(False) + + # 添加控件元素 + self.Combo_NotchesVisible.addItem("显示") + self.Combo_NotchesVisible.addItem("不显示") + + # 连接槽函数至seeting_norchesvisible_Signal + self.Combo_NotchesVisible.currentIndexChanged.connect(self.seeting_norchesvisible_Signal) + + # 将Combo_NotchesVisible[下拉选择框]添加至垂直布局中 + norchesvisible_vertical_layout.addWidget(self.Combo_NotchesVisible) + # 将norchesvisible_vertical_layout[Group]添加至主Group中 + self.norchesvisible_group_box.setLayout(norchesvisible_vertical_layout) + + # 设置项:是否允许转盘循环 + def seeting_notchesVisile(self): + # 是否循环Group + self.notchesVisile_group_box = QGroupBox("循环滚动") + + # 下拉框选择控件 + self.Combo_notchesVisile = QComboBox() + # 该Group内为垂直布局 + notchesVisile_vertical_layout = QVBoxLayout() + # 配置下拉选框是否可以编辑 + self.Combo_NotchesVisible.setEditable(False) + + # 添加控件元素 + self.Combo_notchesVisile.addItem("循环") + self.Combo_notchesVisile.addItem("不循环") + + # 连接槽函数至 + self.Combo_notchesVisile.currentIndexChanged.connect(self.seeting_notchesVisile_Signal) + + # 将Combo_notchesVisile[下拉选择框]添加至垂直布局中 + notchesVisile_vertical_layout.addWidget(self.Combo_notchesVisile) + # 将notchesVisile_vertical_layout[Group]添加至主Group中 + self.notchesVisile_group_box.setLayout(notchesVisile_vertical_layout) + + # 转盘信号槽方法 [回调函数] + def Dialg_Signal(self): + self.control_2.setValue(self.control_1.value()) + + # 设置项:是否显示刻度 [回调函数] + def seeting_norchesvisible_Signal(self): + # 判断索引号:0[显示] 1[不显示] + if self.Combo_NotchesVisible.currentIndex() == 0: + self.control_1.setNotchesVisible(True) + elif self.Combo_NotchesVisible.currentIndex() == 1: + self.control_1.setNotchesVisible(False) + + # 设置项:是否允许转盘循环 [回调函数] + def seeting_notchesVisile_Signal(self): + # 判断索引号:0[循环] 1[不循环] + if self.Combo_notchesVisile.currentIndex() == 0: + self.control_1.setWrapping(True) + elif self.Combo_notchesVisile.currentIndex() == 1: + self.control_1.setWrapping(False) +if __name__ == '__main__': + app = QApplication(sys.argv) + + w = MyWindow() + w.show() + + app.exec() diff --git a/05_pycharm_python_pyqt5学习/.idea/.gitignore b/05_pycharm_python_pyqt5学习/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/05_pycharm_python_pyqt5学习/.idea/002_Python_pyqt5_学习.iml b/05_pycharm_python_pyqt5学习/.idea/002_Python_pyqt5_学习.iml new file mode 100644 index 0000000..1ccb60c --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/002_Python_pyqt5_学习.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/Project_Default.xml b/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..0787f78 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,12 @@ + + + + \ No newline at end of file diff --git a/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/profiles_settings.xml b/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/05_pycharm_python_pyqt5学习/.idea/misc.xml b/05_pycharm_python_pyqt5学习/.idea/misc.xml new file mode 100644 index 0000000..20b6ee1 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/05_pycharm_python_pyqt5学习/.idea/modules.xml b/05_pycharm_python_pyqt5学习/.idea/modules.xml new file mode 100644 index 0000000..b940263 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/05_pycharm_python_pyqt5学习/UserWindow.ui b/05_pycharm_python_pyqt5学习/UserWindow.ui new file mode 100644 index 0000000..2f136a8 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/UserWindow.ui @@ -0,0 +1,106 @@ + + + Form + + + + 0 + 0 + 467 + 145 + + + + 登录器 + + + + + 10 + 30 + 61 + 21 + + + + + Adobe 黑体 Std R + 12 + + + + 用户ID: + + + + + + 80 + 30 + 161 + 20 + + + + + + + 80 + 60 + 161 + 20 + + + + + + + 10 + 60 + 61 + 21 + + + + + Adobe 黑体 Std R + 12 + + + + 密码: + + + + + + 190 + 90 + 51 + 21 + + + + + Adobe 黑体 Std R + 12 + + + + 登录 + + + + + + 250 + 20 + 201 + 111 + + + + + + + diff --git a/05_pycharm_python_pyqt5学习/main.py b/05_pycharm_python_pyqt5学习/main.py new file mode 100644 index 0000000..80adec2 --- /dev/null +++ b/05_pycharm_python_pyqt5学习/main.py @@ -0,0 +1,94 @@ +import json +import sys +import time +import requests + +from PyQt5.QtCore import QThread, pyqtSignal +from PyQt5.QtWidgets import * +from PyQt5 import uic,QtCore + +class LoginThread (QThread): + # 创建自定义信号 + Start_Login_Signal = pyqtSignal(str) + + def __init__(self, signal): + super().__init__() + self.Login_Complete_Signal = signal + + def Login_by_Requests(self, User_Password_Json): + User_Data_Json = json.loads(User_Password_Json) + print(User_Data_Json.get("User_ID")) + print(User_Data_Json.get("User_Password")) + + # 使用Requests模块发送请求 POST + r = requests.post(url="https://service-r80raze1-1300421481.sh.apigw.tencentcs.com/release/Python_Pyqt_Learn", json=User_Data_Json) + print("接受服务器响应:", r.content.decode()) + ret = r.json() + + print("发送信号给UI线程") + self.Login_Complete_Signal.emit(json.dumps(ret)) + + def run(self): + while True: + print ("子线程 running...") + time.sleep(1) + +class UserMainWindow(QWidget): + Login_Status_Signal = pyqtSignal(str) + + def __init__(self): + super().__init__() + self.init_ui() + + def init_ui(self): + self.ui = uic.loadUi("./UserWindow.ui") + + # 提取要操作的控件 + self.Get_Widget() + + # 绑定登录按钮至槽函数:User_Login + self.BTN_Login.clicked.connect(self.User_Login) + # 子线程登录成功后向主线程发送信号 + self.Login_Status_Signal.connect(self.Login_Status) + + # 创建子线程 + self.Login_Thread = LoginThread(self.Login_Status_Signal) + # 将要创建的子线程类中的信号进行绑定 + self.Login_Thread.Start_Login_Signal.connect(self.Login_Thread.Login_by_Requests) + # 子线程开始工作 + self.Login_Thread.start() + + def Get_Widget(self): + # 用户信息输入框 + # 用户ID + self.UserInputID_Edit = self.ui.UserID_Edit + # 用户密码 + self.UserInputPwd_Edit = self.ui.UserPwd_Edit + + # 信息输出框 + self.MessageOutput_Text = self.ui.Message_Text + + # 按钮:用户登录 + self.BTN_Login = self.ui.BTN_Login + + def User_Login(self): + # 获取用户输入文本 + User_ID = self.UserInputID_Edit.text() + User_Password = self.UserInputPwd_Edit.text() + # 发送信号,子线程开始登录 + self.Login_Thread.Start_Login_Signal.emit(json.dumps({"User_ID": User_ID, "User_Password": User_Password})) + + def Login_Status(self, status): + Status_Dict = json.loads(status) + print(Status_Dict.get("errmsg")) + self.MessageOutput_Text.setText(Status_Dict.get("errmsg")) + self.MessageOutput_Text.repaint() + + +if __name__ == '__main__': + QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling) + app = QApplication(sys.argv) + w = UserMainWindow() + # 展示窗口 + w.ui.show() + app.exec() \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/.gitignore b/06_pycharm_python_阿里云Api_DDNS/.idea/.gitignore new file mode 100644 index 0000000..359bb53 --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/.gitignore @@ -0,0 +1,3 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/004_Python_Requist.iml b/06_pycharm_python_阿里云Api_DDNS/.idea/004_Python_Requist.iml new file mode 100644 index 0000000..8437fe6 --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/004_Python_Requist.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/Project_Default.xml b/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..e73fad0 --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,56 @@ + + + + \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/profiles_settings.xml b/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/misc.xml b/06_pycharm_python_阿里云Api_DDNS/.idea/misc.xml new file mode 100644 index 0000000..dc9ea49 --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/.idea/modules.xml b/06_pycharm_python_阿里云Api_DDNS/.idea/modules.xml new file mode 100644 index 0000000..b41354b --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/06_pycharm_python_阿里云Api_DDNS/main.py b/06_pycharm_python_阿里云Api_DDNS/main.py new file mode 100644 index 0000000..485f894 --- /dev/null +++ b/06_pycharm_python_阿里云Api_DDNS/main.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python +#coding=utf-8 + +# 加载核心SDK +from aliyunsdkcore.client import AcsClient +from aliyunsdkcore.acs_exception.exceptions import ClientException +from aliyunsdkcore.acs_exception.exceptions import ServerException + +# 加载获取 、 新增、 更新、 删除接口 +from aliyunsdkalidns.request.v20150109 import DescribeSubDomainRecordsRequest, AddDomainRecordRequest, UpdateDomainRecordRequest, DeleteDomainRecordRequest + +# 加载内置模块 +import json,urllib + +# AccessKey 和 Secret 建议使用 RAM 子账户的 KEY 和 SECRET 增加安全性 +ID = 'xxxxxxx' +SECRET = 'xxxxxx' + +# 地区节点 可选地区取决于你的阿里云帐号等级,普通用户只有四个,分别是杭州、上海、深圳、河北,具体参考官网API +regionId = 'cn-hangzhou' + +# 配置认证信息 +client = AcsClient(ID, SECRET, regionId) + +# 设置主域名 +DomainName = 'binghe.com' + +# 子域名列表 列表参数可根据实际需求增加或减少值 +SubDomainList = ['a', 'b', 'c'] + +# 获取外网IP 三个地址返回的ip地址格式各不相同,3322 的是最纯净的格式, 备选1为 json格式 备选2 为curl方式获取 两个备选地址都需要对获取值作进一步处理才能使用 +def getIp(): + # 备选地址:1, http://pv.sohu.com/cityjson?ie=utf-8 2,curl -L tool.lu/ip + with urllib.request.urlopen('http://www.3322.org/dyndns/getip') as response: + html = response.read() + ip = str(html, encoding='utf-8').replace("\n", "") + return ip + +# 查询记录 +def getDomainInfo(SubDomain): + request = DescribeSubDomainRecordsRequest.DescribeSubDomainRecordsRequest() + request.set_accept_format('json') + + # 设置要查询的记录类型为 A记录 官网支持A / CNAME / MX / AAAA / TXT / NS / SRV / CAA / URL隐性(显性)转发 如果有需要可将该值配置为参数传入 + request.set_Type("A") + + # 指定查记的域名 格式为 'test.binghe.com' + request.set_SubDomain(SubDomain) + + response = client.do_action_with_exception(request) + response = str(response, encoding='utf-8') + + # 将获取到的记录转换成json对象并返回 + return json.loads(response) + +# 新增记录 (默认都设置为A记录,通过配置set_Type可设置为其他记录) +def addDomainRecord(client,value,rr,domainname): + request = AddDomainRecordRequest.AddDomainRecordRequest() + request.set_accept_format('json') + + # request.set_Priority('1') # MX 记录时的必选参数 + request.set_TTL('600') # 可选值的范围取决于你的阿里云账户等级,免费版为 600 - 86400 单位为秒 + request.set_Value(value) # 新增的 ip 地址 + request.set_Type('A') # 记录类型 + request.set_RR(rr) # 子域名名称 + request.set_DomainName(domainname) #主域名 + + # 获取记录信息,返回信息中包含 TotalCount 字段,表示获取到的记录条数 0 表示没有记录, 其他数字为多少表示有多少条相同记录,正常有记录的值应该为1,如果值大于1则应该检查是不是重复添加了相同的记录 + response = client.do_action_with_exception(request) + response = str(response, encoding='utf-8') + relsult = json.loads(response) + return relsult + +# 更新记录 +def updateDomainRecord(client,value,rr,record_id): + request = UpdateDomainRecordRequest.UpdateDomainRecordRequest() + request.set_accept_format('json') + + # request.set_Priority('1') + request.set_TTL('600') + request.set_Value(value) # 新的ip地址 + request.set_Type('A') + request.set_RR(rr) + request.set_RecordId(record_id) # 更新记录需要指定 record_id ,该字段为记录的唯一标识,可以在获取方法的返回信息中得到该字段的值 + + response = client.do_action_with_exception(request) + response = str(response, encoding='utf-8') + return response + +# 删除记录 +def delDomainRecord(client,subdomain): + info = getDomainInfo(subdomain) + if info['TotalCount'] == 0: + print('没有相关的记录信息,删除失败!') + elif info["TotalCount"] == 1: + print('准备删除记录') + request = DeleteDomainRecordRequest.DeleteDomainRecordRequest() + request.set_accept_format('json') + + record_id = info["DomainRecords"]["Record"][0]["RecordId"] + request.set_RecordId(record_id) # 删除记录需要指定 record_id ,该字段为记录的唯一标识,可以在获取方法的返回信息中得到该字段的值 + result = client.do_action_with_exception(request) + print('删除成功,返回信息:') + print(result) + else: + # 正常不应该有多条相同的记录,如果存在这种情况,应该手动去网站检查核实是否有操作失误 + print("存在多个相同子域名解析记录值,请核查后再操作!") + +# 有记录则更新,没有记录则新增 +def setDomainRecord(client,value,rr,domainname): + info = getDomainInfo(rr + '.' + domainname) + if info['TotalCount'] == 0: + print('准备添加新记录') + add_result = addDomainRecord(client,value,rr,domainname) + print(add_result) + elif info["TotalCount"] == 1: + print('准备更新已有记录') + record_id = info["DomainRecords"]["Record"][0]["RecordId"] + cur_ip = getIp() + old_ip = info["DomainRecords"]["Record"][0]["Value"] + if cur_ip == old_ip: + print ("新ip与原ip相同,不更新!") + else: + update_result = updateDomainRecord(client,value,rr,record_id) + print('更新成功,返回信息:') + print(update_result) + else: + # 正常不应该有多条相同的记录,如果存在这种情况,应该手动去网站检查核实是否有操作失误 + print("存在多个相同子域名解析记录值,请核查删除后再操作!") + + +IP = getIp() + +# 循环子域名列表进行批量操作 +for x in SubDomainList: + setDomainRecord(client,IP,x,DomainName) + +# 删除记录测试 +# delDomainRecord(client,'b.jsoner.com') + +# 新增或更新记录测试 +# setDomainRecord(client,'192.168.3.222','a',DomainName) + +# 获取记录测试 +# print (getDomainInfo(DomainName, 'y')) + +# 批量获取记录测试 +# for x in SubDomainList: +# print (getDomainInfo(DomainName, x)) + +# 获取外网ip地址测试 +# print ('(' + getIp() + ')') \ No newline at end of file