-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
129 lines (106 loc) · 4.92 KB
/
server.py
File metadata and controls
129 lines (106 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QVBoxLayout, QWidget, QLabel, QLineEdit, QTextEdit
from PyQt5.QtCore import pyqtSlot
import socket
import threading
class Server(QMainWindow):
def __init__(self):
super().__init__()
self.initUI()
self.server = None
self.clients = []
self.is_running = False
def initUI(self):
self.setWindowTitle('Server Control Panel')
self.setGeometry(100, 100, 500, 700)
self.layout = QVBoxLayout()
self.infoLabel = QLabel('Server is not running')
self.layout.addWidget(self.infoLabel)
self.clientsLabel = QLabel('Connected Clients:')
self.layout.addWidget(self.clientsLabel)
self.startBtn = QPushButton('Start Server', self)
self.startBtn.clicked.connect(self.start_server)
self.layout.addWidget(self.startBtn)
self.stopBtn = QPushButton('Stop Server', self)
self.stopBtn.clicked.connect(self.stop_server)
self.layout.addWidget(self.stopBtn)
self.messageInput = QLineEdit(self)
self.messageInput.setText('sub01_W1')
self.layout.addWidget(self.messageInput)
self.sendCustomMsgBtn = QPushButton('Send Message', self)
self.sendCustomMsgBtn.clicked.connect(self.send_custom_message)
self.layout.addWidget(self.sendCustomMsgBtn)
self.sendBtn = QPushButton('Send Start Command', self)
self.sendBtn.clicked.connect(self.send_start_click)
self.layout.addWidget(self.sendBtn)
self.sendDoubleClickBtn = QPushButton('Send Stop Command', self)
self.sendDoubleClickBtn.clicked.connect(self.send_stop_click)
self.layout.addWidget(self.sendDoubleClickBtn)
# GUI Enhancement: Add a log text area for server actions and messages
self.logTextArea = QTextEdit(self)
self.logTextArea.setReadOnly(True)
self.layout.addWidget(self.logTextArea)
container = QWidget()
container.setLayout(self.layout)
self.setCentralWidget(container)
@pyqtSlot()
def start_server(self):
if not self.is_running:
self.is_running = True
self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.server.bind(('0.0.0.0', 9999))
self.server.listen(5)
threading.Thread(target=self.accept_connections, daemon=True).start()
self.infoLabel.setText('Server is running on port 9999')
self.log_message("Server started on port 9999")
@pyqtSlot()
def stop_server(self):
if self.is_running:
self.is_running = False
for client in self.clients:
client.close()
self.server.close()
self.clients.clear()
self.clientsLabel.setText('Connected Clients:')
self.infoLabel.setText('Server is not running')
self.log_message("Server stopped.")
@pyqtSlot()
def send_start_click(self):
if self.is_running:
for client in self.clients:
client.sendall(b'start_click')
self.infoLabel.setText('Start click command sent to all clients')
self.log_message('Start click command sent to all clients')
@pyqtSlot()
def send_stop_click(self):
if self.is_running:
for client in self.clients:
client.sendall(b'stop_click')
self.infoLabel.setText('Stop click command sent to all clients')
self.log_message('Stop click command sent to all clients')
@pyqtSlot()
def send_custom_message(self):
if self.is_running:
custom_msg = self.messageInput.text().encode('utf-8')
for client in self.clients:
client.sendall(custom_msg)
self.infoLabel.setText('Custom message sent to all clients')
self.log_message(f'Custom message "{self.messageInput.text()}" sent to all clients')
def accept_connections(self):
while self.is_running:
client, addr = self.server.accept()
self.clients.append(client)
self.update_clients_label()
self.log_message(f"Connection established with {addr}")
def update_clients_label(self):
clients_info = 'Connected Clients:\n' + '\n'.join([f"{idx+1}. {c.getpeername()[0]}" for idx, c in enumerate(self.clients)])
self.clientsLabel.setText(clients_info)
def log_message(self, message):
"""Method to append messages to the log text area."""
self.logTextArea.append(message)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Server()
ex.show()
sys.exit(app.exec_())