-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsockets.py
73 lines (58 loc) · 2.27 KB
/
sockets.py
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
import socket
import subprocess
import socket
import subprocess
class Command:
def __init__(self):
self.commands = {
"terminal\n": self.command_terminal,
"google\n": self.command_google,
"youtube\n": self.command_youtube,
"calculator\n": self.command_calculator,
"suspend\n": self.command_suspend
}
def command_terminal(self):
subprocess.run(['gnome-terminal'])
def command_google(self):
subprocess.run(['google-chrome'])
def command_youtube(self):
subprocess.run(['google-chrome', 'https://www.youtube.com'])
def command_calculator(self):
subprocess.run(['gnome-calculator'])
def command_suspend(self):
subprocess.run(['sudo', 'pm-suspend'])
class Server(Command):
def __init__(self, ip_address, port_number):
super().__init__()
self.ip_address = ip_address
self.port_number = port_number
self.pending_num = 1
self.data_buffer = ""
self.server_listening_socket = None
self.server_connection_socket = None
def server_init(self):
# Creating the server socket
self.server_listening_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.server_listening_socket.bind((self.ip_address, self.port_number))
self.server_listening_socket.listen()
print(f"Server listening on {self.ip_address}:{self.port_number}")
while True:
self.server_connection_socket, _ = self.server_listening_socket.accept()
print("Client connected")
data = self.server_connection_socket.recv(1024).decode()
self.data_buffer = data.lower()
print("Message from client:", self.data_buffer)
self.server_execute_command()
self.server_connection_socket.close()
print("Client disconnected")
def server_send(self, message):
self.server_connection_socket.sendall(message.encode())
def server_execute_command(self):
command = self.commands.get(self.data_buffer)
if command:
command()
else:
print('Invalid command received from client')
if __name__ == "__main__":
my_server = Server("192.168.1.21", 8080)
my_server.server_init()