parent
db66840067
commit
d6db0ce71f
@ -1,31 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <mysql/mysql.h>
|
||||
|
||||
int main() {
|
||||
MYSQL *conn;
|
||||
conn = mysql_init(NULL);
|
||||
|
||||
if (conn == NULL) {
|
||||
std::cerr << "MySQL init failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *server = "localhost";
|
||||
const char *user = "rtsw";
|
||||
const char *password = "123456";
|
||||
const char *database = "nginxdb";
|
||||
unsigned int port = 3306; // 使用云数据库提供的端口
|
||||
|
||||
if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) {
|
||||
std::cerr << "Connection error: " << mysql_error(conn) << std::endl;
|
||||
mysql_close(conn);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Successfully connected to the database" << std::endl;
|
||||
|
||||
// ... 执行数据库操作 ...
|
||||
|
||||
mysql_close(conn);
|
||||
return 0;
|
||||
}
|
@ -1,86 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <cstring>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define PORT 8080
|
||||
#define MAX_CLIENTS 5
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
// 声明 handle_client 函数
|
||||
void handle_client(int client_socket);
|
||||
|
||||
int main() {
|
||||
int server_fd, new_socket;
|
||||
struct sockaddr_in server_addr, client_addr;
|
||||
socklen_t client_len = sizeof(client_addr);
|
||||
char buffer[BUFFER_SIZE];
|
||||
int opt = 1;
|
||||
|
||||
// 创建套接字
|
||||
server_fd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (server_fd < 0) {
|
||||
perror("socket creation failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// 设置选项,允许重用地址
|
||||
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) {
|
||||
perror("setsockopt failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_addr.s_addr = INADDR_ANY;
|
||||
server_addr.sin_port = htons(PORT);
|
||||
|
||||
// 绑定
|
||||
if (bind(server_fd, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
perror("bind failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// 监听
|
||||
if (listen(server_fd, MAX_CLIENTS) < 0) {
|
||||
perror("listen failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "Server listening on port " << PORT << std::endl;
|
||||
|
||||
while (true) {
|
||||
client_len = sizeof(client_addr);
|
||||
new_socket = accept(server_fd, (struct sockaddr *)&client_addr, &client_len);
|
||||
if (new_socket < 0) {
|
||||
perror("accept failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::cout << "New client connected from " << inet_ntoa(client_addr.sin_addr) << " port " << ntohs(client_addr.sin_port) << std::endl;
|
||||
|
||||
// 创建并启动线程处理客户端
|
||||
std::thread(handle_client, new_socket).detach();
|
||||
}
|
||||
|
||||
close(server_fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 定义 handle_client 函数
|
||||
void handle_client(int client_socket) {
|
||||
while (true) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
memset(buffer, 0, BUFFER_SIZE); // 清空缓冲区
|
||||
int nbytes = read(client_socket, buffer, BUFFER_SIZE);
|
||||
if (nbytes <= 0) {
|
||||
std::cout << "Client disconnected." << std::endl;
|
||||
close(client_socket);
|
||||
break;
|
||||
}
|
||||
std::cout << "Received message from client: " << buffer << std::endl;
|
||||
// 这里可以添加更多的处理逻辑
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <mysql/mysql.h>
|
||||
|
||||
int main() {
|
||||
MYSQL *conn;
|
||||
conn = mysql_init(NULL);
|
||||
|
||||
if (conn == NULL) {
|
||||
std::cerr << "MySQL init failed" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char *server = "localhost";
|
||||
const char *user = "rtsw";
|
||||
const char *password = "123456";
|
||||
const char *database = "nginxdb";
|
||||
unsigned int port = 3306;
|
||||
|
||||
if (mysql_real_connect(conn, server, user, password, database, port, NULL, 0) == NULL) {
|
||||
std::cerr << "Connection error: " << mysql_error(conn) << std::endl;
|
||||
mysql_close(conn);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::cout << "Successfully connected to the database" << std::endl;
|
||||
|
||||
// 插入数据
|
||||
const char *insert_query = "INSERT INTO users (username, email) VALUES ('newuser', 'newuser@example.com')";
|
||||
if (mysql_query(conn, insert_query)) {
|
||||
std::cerr << "Insert error: " << mysql_error(conn) << std::endl;
|
||||
} else {
|
||||
std::cout << "Insert successful" << std::endl;
|
||||
}
|
||||
|
||||
// 查询数据
|
||||
const char *select_query = "SELECT * FROM users";
|
||||
MYSQL_RES *result = mysql_store_result(conn);
|
||||
if (result) {
|
||||
MYSQL_ROW row;
|
||||
while ((row = mysql_fetch_row(result))) {
|
||||
std::cout << "id: " << row[0] << ", username: " << row[1] << ", email: " << row[2] << std::endl;
|
||||
}
|
||||
mysql_free_result(result);
|
||||
} else {
|
||||
std::cerr << "Select error: " << mysql_error(conn) << std::endl;
|
||||
}
|
||||
|
||||
// 更新数据
|
||||
const char *update_query = "UPDATE users SET email = 'newuser_updated@example.com' WHERE username = 'newuser'";
|
||||
if (mysql_query(conn, update_query)) {
|
||||
std::cerr << "Update error: " << mysql_error(conn) << std::endl;
|
||||
} else {
|
||||
std::cout << "Update successful" << std::endl;
|
||||
}
|
||||
|
||||
// 删除数据
|
||||
const char *delete_query = "DELETE FROM users WHERE username = 'newuser'";
|
||||
if (mysql_query(conn, delete_query)) {
|
||||
std::cerr << "Delete error: " << mysql_error(conn) << std::endl;
|
||||
} else {
|
||||
std::cout << "Delete successful" << std::endl;
|
||||
}
|
||||
|
||||
mysql_close(conn);
|
||||
return 0;
|
||||
}
|
@ -1,22 +0,0 @@
|
||||
#ifndef CONNECTOR_CONTROLLER_H
|
||||
#define CONNECTOR_CONTROLLER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <cstring>
|
||||
|
||||
// 函数声明
|
||||
void StartHttpServer();
|
||||
void ConnectToGateway(const std::string& ip, int port);
|
||||
void KeepConnection(int sock);
|
||||
void HandleHttpRequest(int client_socket);
|
||||
|
||||
#endif // CONNECTOR_CONTROLLER_H
|
@ -1,10 +0,0 @@
|
||||
#ifndef CONNECTOR_DATABASE_H
|
||||
#define CONNECTOR_DATABASE_H
|
||||
|
||||
#include <iostream>
|
||||
#include <mysql/mysql.h>
|
||||
|
||||
// 函数声明
|
||||
void DatabaseOperation();
|
||||
|
||||
#endif // CONNECTOR_DATABASE_H
|
@ -1,35 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "connector_controller.h"
|
||||
#include "connector_database.h"
|
||||
|
||||
int main() {
|
||||
std::string command;
|
||||
bool running = true;
|
||||
|
||||
while (running) {
|
||||
std::cout << "Enter command (start_http_server/connect_gateway/database_operation/exit): ";
|
||||
std::cin >> command;
|
||||
|
||||
if (command == "start_http_server") {
|
||||
StartHttpServer();
|
||||
} else if (command == "connect_gateway") {
|
||||
std::string ip;
|
||||
int port;
|
||||
std::cout << "Enter the gateway IP address: "<<std::endl;
|
||||
std::cin>>ip; // 使用getline以获取包含空格的IP地址
|
||||
std::cout << "Enter the gateway port: "<<std::endl;
|
||||
std::cin >> port;
|
||||
ConnectToGateway(ip, port);
|
||||
} else if (command == "database_operation") {
|
||||
DatabaseOperation();
|
||||
} else if (command == "exit") {
|
||||
running = false;
|
||||
} else {
|
||||
std::cout << "Unknown command" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "Exiting program." << std::endl;
|
||||
return 0;
|
||||
}
|
Binary file not shown.
@ -1,125 +0,0 @@
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <atomic>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <unistd.h>
|
||||
#include <arpa/inet.h>
|
||||
|
||||
#define BUFFER_SIZE 1024
|
||||
|
||||
std::vector<std::thread> threads; // 存储所有连接线程
|
||||
std::map<int, int> sockets; // 存储套接字
|
||||
std::mutex sockets_mutex; // 用于同步访问 sockets
|
||||
std::atomic<bool> exit_flag(false); // 原子退出标志
|
||||
|
||||
|
||||
// 维护连接的函数
|
||||
void KeepConnection(int sock) {
|
||||
char buffer[BUFFER_SIZE];
|
||||
int nbytes;
|
||||
while (!exit_flag) {
|
||||
nbytes = recv(sock, buffer, BUFFER_SIZE, 0);
|
||||
if (nbytes <= 0) {
|
||||
std::cout << "Gateway disconnected" << std::endl;
|
||||
break;
|
||||
}
|
||||
std::cout << "Received from gateway (Socket " << sock << "): " << buffer << std::endl;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(sockets_mutex);
|
||||
sockets.erase(sock); // 从集合中移除套接字
|
||||
}
|
||||
close(sock);
|
||||
}
|
||||
// 连接到网关的函数
|
||||
void ConnectToGateway(const std::string& ip, int port) {
|
||||
int sock = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (sock < 0) {
|
||||
perror("Could not create socket");
|
||||
return;
|
||||
}
|
||||
|
||||
struct sockaddr_in server_addr;
|
||||
server_addr.sin_family = AF_INET;
|
||||
server_addr.sin_port = htons(port);
|
||||
if (inet_pton(AF_INET, ip.c_str(), &server_addr.sin_addr) <= 0) {
|
||||
perror("Invalid address");
|
||||
close(sock);
|
||||
return;
|
||||
}
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) < 0) {
|
||||
perror("Connection to the server failed");
|
||||
close(sock);
|
||||
return;
|
||||
}
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(sockets_mutex);
|
||||
sockets[sock] = 1; // 将套接字添加到集合中
|
||||
}
|
||||
|
||||
std::cout << "Connected to gateway at " << ip << ":" << port << " (Socket " << sock << ")" << std::endl;
|
||||
|
||||
// 在新线程中维护连接
|
||||
threads.emplace_back(KeepConnection, sock);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
std::string command, ip;
|
||||
int port;
|
||||
bool in_connect_mode = false;
|
||||
|
||||
while (true) {
|
||||
std::cout << "Enter command (connect/exitconnect/exit): ";
|
||||
std::cin >> command;
|
||||
|
||||
if (command == "connect") {
|
||||
if (in_connect_mode) {
|
||||
std::cout << "Already in connect mode." << std::endl;
|
||||
continue;
|
||||
}
|
||||
in_connect_mode = true; // 进入连接模式
|
||||
std::cout << "Enter the gateway IP address: ";
|
||||
std::cin >> ip;
|
||||
std::cout << "Enter the gateway port: ";
|
||||
std::cin >> port;
|
||||
ConnectToGateway(ip, port);
|
||||
} else if (command == "exitconnect") {
|
||||
if (!in_connect_mode) {
|
||||
std::cout << "Not in connect mode." << std::endl;
|
||||
continue;
|
||||
}
|
||||
in_connect_mode = false; // 退出连接模式
|
||||
std::cout << "Exiting connect mode." << std::endl;
|
||||
} else if (command == "exit") {
|
||||
exit_flag = true; // 设置退出标志
|
||||
std::cout << "Exiting program." << std::endl;
|
||||
// 关闭所有套接字
|
||||
for (auto& sock_pair : sockets) {
|
||||
shutdown(sock_pair.first, SHUT_RDWR); // 关闭套接字的发送和接收
|
||||
close(sock_pair.first);
|
||||
}
|
||||
sockets.clear();
|
||||
// 等待所有线程结束
|
||||
for (auto& thread : threads) {
|
||||
if (thread.joinable()) {
|
||||
thread.join();
|
||||
}
|
||||
}
|
||||
threads.clear();
|
||||
break; // 退出主循环
|
||||
} else {
|
||||
std::cout << "Unknown command" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
# 默认忽略的文件
|
||||
/shelf/
|
||||
/workspace.xml
|
@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="PYTHON_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||
</content>
|
||||
<orderEntry type="jdk" jdkName="Python 3.12 (G:)" jdkType="Python SDK" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
<component name="PackageRequirementsSettings">
|
||||
<option name="requirementsPath" value="" />
|
||||
</component>
|
||||
<component name="PyDocumentationSettings">
|
||||
<option name="format" value="EPYTEXT" />
|
||||
<option name="myDocStringFormat" value="Epytext" />
|
||||
</component>
|
||||
</module>
|
@ -0,0 +1,6 @@
|
||||
<component name="InspectionProjectProfileManager">
|
||||
<settings>
|
||||
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||
<version value="1.0" />
|
||||
</settings>
|
||||
</component>
|
@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="Black">
|
||||
<option name="sdkName" value="Python 3.12 (controller)" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (G:)" project-jdk-type="Python SDK" />
|
||||
</project>
|
@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/controller.iml" filepath="$PROJECT_DIR$/.idea/controller.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: confControl.py
|
||||
# @time: 2024/6/21 11:18
|
||||
# @desc:
|
||||
|
||||
|
||||
#TODO 通过ssh在线更改nginx的conf文件
|
Binary file not shown.
@ -0,0 +1,53 @@
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: Server.py
|
||||
# @time: 2024/6/19 9:22
|
||||
# @desc:
|
||||
import json
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
from MessageHandler.PreDataProcessor import *
|
||||
import time
|
||||
|
||||
class CORSHTTPRequestHandler(BaseHTTPRequestHandler):
|
||||
def do_OPTIONS(self):
|
||||
self.send_response(204)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
|
||||
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
|
||||
self.end_headers()
|
||||
|
||||
def do_POST(self):
|
||||
content_length = int(self.headers['Content-Length'])
|
||||
post_data = self.rfile.read(content_length)
|
||||
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.end_headers()
|
||||
|
||||
print("Request line:", self.requestline)
|
||||
print("Headers:", self.headers)
|
||||
print("Body:", post_data.decode('utf-8'))
|
||||
|
||||
# 处理程序
|
||||
MessageProcessor = preDataProcessor(post_data)
|
||||
# response = {"POST": "request received"}
|
||||
# self.wfile.write(str(response).encode('utf-8'))
|
||||
# time.sleep(1)
|
||||
backMessage =MessageProcessor.returnMessage
|
||||
response = {"POST": backMessage}
|
||||
|
||||
self.wfile.write(str(json.dumps(response)).encode('utf-8'))
|
||||
# self.wfile.write("{\"POST\": \"request received\"}".encode('utf-8'))
|
||||
|
||||
def do_GET(self):
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/plain')
|
||||
self.send_header('Access-Control-Allow-Origin', '*')
|
||||
self.end_headers()
|
||||
|
||||
self.wfile.write(b"Hello, world!")
|
||||
|
Binary file not shown.
@ -0,0 +1,74 @@
|
||||
#!/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: MachineLearningDivider.py
|
||||
# @time: 2024/6/26 8:21
|
||||
# @desc:利用随机森林法进行模型训练,能够通过平均响应时间、故障率等数据计算出服务器的健康状态
|
||||
|
||||
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.ensemble import RandomForestClassifier
|
||||
from sklearn.metrics import classification_report, accuracy_score
|
||||
import pickle
|
||||
# 假设我们有以下数据集
|
||||
X = [
|
||||
[0.3, 0.005], # 服务器特征:平均响应时间和故障率
|
||||
[2.5, 0.03],
|
||||
[0.7, 0.045],
|
||||
[1.2, 0.002]
|
||||
]
|
||||
y = ['良好', '差', '差', '良好'] # 对应的健康状态标签
|
||||
|
||||
# 将健康状态标签转换为数值
|
||||
label_mapping = {'一般': 0, '良好': 1, '差': 2, '极差': 3}
|
||||
y_encoded = [label_mapping[label] for label in y]
|
||||
|
||||
# 划分训练集和测试集
|
||||
X_train, X_test, y_train, y_test = train_test_split(X, y_encoded, test_size=0.25, random_state=42)
|
||||
|
||||
# 选择模型,这里使用随机森林分类器
|
||||
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
||||
|
||||
# 训练模型
|
||||
model.fit(X_train, y_train)
|
||||
|
||||
# 预测测试集
|
||||
y_pred = model.predict(X_test)
|
||||
|
||||
# 评估模型
|
||||
print(classification_report(y_test, y_pred))
|
||||
print("Accuracy:", accuracy_score(y_test, y_pred))
|
||||
|
||||
with open('server_health_model.pkl', 'wb') as file:
|
||||
pickle.dump(model, file)
|
||||
|
||||
|
||||
# 保存模型
|
||||
with open('server_health_model.pkl', 'wb') as file:
|
||||
pickle.dump(model, file)
|
||||
|
||||
# 定义一个函数来加载模型并进行预测
|
||||
def load_model_and_predict(new_data):
|
||||
with open('server_health_model.pkl', 'rb') as file:
|
||||
loaded_model = pickle.load(file)
|
||||
predictions = loaded_model.predict(new_data)
|
||||
return predictions
|
||||
|
||||
# 定义一个函数来将预测结果转换为健康等级
|
||||
def predict_health_status(new_data):
|
||||
predictions = load_model_and_predict(new_data)
|
||||
# 创建逆向映射字典
|
||||
inverse_label_mapping = {value: key for key, value in label_mapping.items()}
|
||||
# 使用逆向映射字典转换预测结果
|
||||
health_status = [inverse_label_mapping[pred] for pred in predictions]
|
||||
return health_status
|
||||
|
||||
# 测试函数
|
||||
def testcase():
|
||||
new_data = [[0.4, 0.01]] # 新的服务器数据
|
||||
health_status = predict_health_status(new_data)
|
||||
print("预测的健康状态:", health_status)
|
||||
|
||||
testcase()
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,2 @@
|
||||
dic = {'a':1,'b':2,'c':3}
|
||||
for i in dic.keys():
|
@ -0,0 +1,58 @@
|
||||
# !/usr/bin/env python
|
||||
# encoding: utf-8
|
||||
# @author: 原凯峰
|
||||
# @contact: 2894340009@qq.com
|
||||
# @software: pycharm
|
||||
# @file: tryselect.py
|
||||
# @time: 2024/6/22 11:32
|
||||
# @desc:
|
||||
|
||||
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
import json
|
||||
|
||||
# 数据库配置信息
|
||||
host = ''
|
||||
database = 'nginxdb'
|
||||
user = 'rtsw'
|
||||
password = '123456'
|
||||
|
||||
# SQL查询语句
|
||||
sql_query = "SELECT * FROM tasks" # 替换成你的表名和查询条件
|
||||
|
||||
# 连接到数据库
|
||||
try:
|
||||
connection = mysql.connector.connect(host=host,
|
||||
database=database,
|
||||
user=user,
|
||||
password=password)
|
||||
if connection.is_connected():
|
||||
cursor = connection.cursor()
|
||||
|
||||
# 执行查询语句
|
||||
cursor.execute(sql_query)
|
||||
|
||||
# 获取所有查询结果
|
||||
rows = cursor.fetchall()
|
||||
|
||||
# 将查询结果转换为字典列表
|
||||
# 假设你的表有列名为'id', 'name', 'age'等
|
||||
columns = [i[0] for i in cursor.description] # 获取列名
|
||||
data_list = [dict(zip(columns, row)) for row in rows] # 将每行数据转换为字典
|
||||
|
||||
# 将字典列表转换为JSON格式
|
||||
json_data = json.dumps(data_list, ensure_ascii=False)
|
||||
|
||||
print(json_data) # 打印JSON格式的数据
|
||||
print(data_list[0]["subject"])
|
||||
|
||||
except Error as e:
|
||||
print("Error while connecting to MySQL", e)
|
||||
|
||||
finally:
|
||||
# 关闭数据库连接
|
||||
if connection.is_connected():
|
||||
cursor.close()
|
||||
connection.close()
|
||||
print("MySQL connection is closed")
|
@ -0,0 +1,21 @@
|
||||
CREATE TABLE `Servers` (
|
||||
`server_name` VARCHAR(32) PRIMARY KEY,
|
||||
`status VARCHAR(32) NOT NULL`,
|
||||
`upstream` VARCHAR(32),
|
||||
`detect_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`responseTime` varchar(255) NOT NULL COMMENT '响应时间',
|
||||
`protocol` varchar(32) NOT NULL COMMENT '通信协议名称',
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COMMENT = '服务器列表';
|
||||
|
||||
|
||||
CREATE TABLE `ServerHealthLog` (
|
||||
`health_id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`server_name` VARCHAR(32),
|
||||
`upstream` VARCHAR(32),
|
||||
`detect_date` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
`status` VARCHAR(32) NOT NULL,
|
||||
`responseTime` varchar(255) NOT NULL COMMENT '响应时间',
|
||||
`protocol` varchar(32) NOT NULL COMMENT '通信协议名称',
|
||||
FOREIGN KEY (`server_name`) REFERENCES Servers(`server_name`),
|
||||
INDEX `check_time_index` (`check_time`)
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COMMENT = '服务器历史信息记录表';
|
@ -0,0 +1,11 @@
|
||||
CREATE TABLE IF NOT EXISTS `user` (
|
||||
`username` varchar(255) NOT NULL,
|
||||
`real_name` varchar(255) NOT NULL COMMENT '真名',
|
||||
`ID` varchar(255) NOT NULL COMMENT '身份证号',
|
||||
`password` varchar(255) NOT NULL COMMENT '密码',
|
||||
`tel` varchar(255) NOT NULL COMMENT '电话号码',
|
||||
PRIMARY KEY (`id`),
|
||||
) DEFAULT CHARACTER SET = utf8mb4 COMMENT = '用户信息列表';
|
||||
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,187 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>注册页面</title>
|
||||
<!-- 引入格式文件-->
|
||||
<!-- <link rel="stylesheet" href="html&css.css"> -->
|
||||
</head>
|
||||
<body>
|
||||
<style>
|
||||
*{
|
||||
margin: 0px;/*所有的外边距为0*/
|
||||
padding: 0px;/*所有的内边距为0*/
|
||||
box-sizing: border-box;/*规定两个并排的带边框的框*/
|
||||
}
|
||||
table{
|
||||
text-align: center;
|
||||
}
|
||||
body{
|
||||
background: url("./assets/images/button.jpg")no-repeat center;
|
||||
padding-top: 25px;
|
||||
}
|
||||
.rg_layout{
|
||||
width: 900px;
|
||||
height: 500px;
|
||||
border: 8px solid #EEEEEE;/*solid 定义实线*/
|
||||
background-color: white;
|
||||
margin: auto;
|
||||
}
|
||||
.rg_left{
|
||||
float: none;
|
||||
text-align: center;
|
||||
margin: 15px;
|
||||
}
|
||||
.rg_left>p:first-child{
|
||||
color: #FFD026;
|
||||
font-size: 20px;
|
||||
}
|
||||
.rg_left>p:last-child{
|
||||
color: #A6A6A6;
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.rg_center{
|
||||
float: left;
|
||||
}
|
||||
|
||||
.rg_right{
|
||||
float: none;
|
||||
margin: 250px;
|
||||
padding-left: 10px;
|
||||
white-space:nowrap;
|
||||
}
|
||||
.rg_right p{
|
||||
font-size: 15px;
|
||||
}
|
||||
.rg_right p a{
|
||||
color: coral;
|
||||
}
|
||||
.td_left{
|
||||
padding-left: 250px;
|
||||
width: 100px;
|
||||
text-align: center;
|
||||
height: 45px;
|
||||
white-space:nowrap;
|
||||
}
|
||||
.td_right{
|
||||
padding-left: 40px;
|
||||
text-align: center;
|
||||
white-space:nowrap;
|
||||
}
|
||||
.bt_center{
|
||||
padding-left: 310px;
|
||||
}
|
||||
#username,#real_name,#ID,#password,#tel,#birthday,#checkcode{
|
||||
width: 251px;
|
||||
height: 32px;
|
||||
border: 1px solid #A6A6A6;
|
||||
/*设置边框圆角*/
|
||||
border-radius: 5px;
|
||||
padding-left: 10px;
|
||||
}
|
||||
#checkcode{
|
||||
width: 110px;
|
||||
}
|
||||
#img_check{
|
||||
height: 32px;
|
||||
vertical-align: middle;/*设置图片的位置垂直居中*/
|
||||
}
|
||||
#btn_sub{
|
||||
width: 100px;
|
||||
height: 40px;
|
||||
background-color: #FFD026;
|
||||
border: 1px solid #FFD026;
|
||||
padding-left: 10px;
|
||||
}
|
||||
|
||||
</style>
|
||||
<div class="rg_layout">
|
||||
<div class="rg_left">
|
||||
<p>新用户注册</p>
|
||||
<p>USER REGISTER</p>
|
||||
</div>
|
||||
<div class="rg_center">
|
||||
<div class="rg_form">
|
||||
<form class="ant-form" action="#" method="post">
|
||||
<table>
|
||||
<tr><!--label 标签的作用是当点击文字也会跳到文本输出框-->
|
||||
<!--for属性与ID属性对应规定 label 绑定到哪个表单元素。-->
|
||||
<td class="td_left"><label for="username">用户名</label> </td>
|
||||
<td class="td_right"><input type="text" name="username" id="username"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="td_left"><label for="real_name">姓名</label> </td>
|
||||
<td class="td_right"><input type="text" name="real_name" id="real_name"> </td>
|
||||
</tr>
|
||||
<tr><!--label 标签的作用是当点击文字也会跳到文本输出框-->
|
||||
<td class="td_left"><label for="ID">身份证号</label> </td>
|
||||
<td class="td_right"><input type="text" name="ID" id="ID"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="td_left"><label for="password">密码</label> </td>
|
||||
<td class="td_right"><input type="password" name="password" id="password"> </td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="td_left"><label for="tel">再次输入密码</label> </td>
|
||||
<td class="td_right"><input type="password" name="tel" id="tel"> </td>
|
||||
</tr>
|
||||
|
||||
|
||||
|
||||
<tr>
|
||||
<td colspan="2" align="center" class="bt_center">
|
||||
<input type="submit" value="注册" id="btn_sub">
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
<div class="rg_right">
|
||||
<p><a href="LoginUI.html">返回登录界面</a></p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
// 获取表单元素
|
||||
var form = document.querySelector('.ant-form');
|
||||
|
||||
// 为表单添加提交事件监听器
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault(); // 阻止表单的默认提交行为
|
||||
|
||||
// 收集表单数据
|
||||
var formData = {
|
||||
//question_kind: document.getElementsByName('question_kind').value,
|
||||
ID:document.getElementById('ID').value,
|
||||
password: document.getElementById('password').value,
|
||||
real_name:document.getElementById('real_name').value,
|
||||
source:"frontend",
|
||||
tel:document.getElementById('tel').value,
|
||||
type:"register_message",
|
||||
username: document.getElementById('username').value,
|
||||
};
|
||||
console.log(formData);
|
||||
fetch('http://47.96.136.178:8080', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(formData) // 将表单数据转换为JSON字符串
|
||||
})
|
||||
.then(response => response.json()) // 转换响应为JSON
|
||||
.then(data => {
|
||||
console.log('Success:', data);
|
||||
alert('反馈成功提交!');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
alert('提交失败,请稍后重试!');
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</html>
|
Loading…
Reference in new issue