-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerApp.py
116 lines (100 loc) · 4.16 KB
/
ServerApp.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
import socket
from select import select
from ARappServer import HandlerRequest
from ARappServer.Authentification import checkRSA_PrivateKey, WrongRSA_Key
from ARappServer.encryptionDES import WrongDES_Key, checkDES_Key
import datetime
HOST, PORT = 'localhost', 50000
# HOST, PORT = '25.36.227.49', 9090
#HOST, PORT = '25.47.76.161', 9090
# HOST, PORT = "192.168.43.2", 50000
tasks = [] # тут должен использоваться модуль
to_read = {}
to_write = {}
# просто пауза
def pause():
input("\nPress the <ENTER> key to continue...")
def server():
'''
Серверный модуль, отвечающий за установление соединения с клиентами
ассинхронный
:return: None
'''
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind((HOST, PORT))
server_socket.listen()
while True:
yield ('read', server_socket)
client_socket, addr = server_socket.accept()
# print('Connection from ', addr)
tasks.append(client(client_socket))
def client(client_socket):
'''
Клиентский модуль, отвечающий за передачу данных от(к) клиента(у)
ассинхронный
:param client_socket: <class 'socket.socket'> принимает объект сокета,
через который клиент общается с сервером
:return: None
'''
while True:
yield ('read', client_socket)
request = client_socket.recv(4096)
# print(request)
if not request:
break
else:
print("--------- Start of transmission ----------"
f"------------\nClient address: {client_socket.getpeername()[0]}:"
f"{client_socket.getpeername()[1]}\n"
f"[----- S <- C -----] {datetime.datetime.now().isoformat('|', 'microseconds')}\n"
f"Data transmission from client to server: \n\t{request.decode()}")
response = HandlerRequest.loadMessage(request.decode()) # block process
# msg = str(response).encode()
msg = response
yield ('write', client_socket)
# print("Responce: {}".format())
client_socket.send(msg)
print(f"[----- S -> C -----] {datetime.datetime.now().isoformat('|', 'microseconds')}\n"
f"Data transmission from client to server: \n\t{msg.decode()}\n"
"---------- End of transmission -----------\n")
client_socket.close()
def event_loop():
while any([tasks, to_read, to_write]):
while not tasks:
# print(f'Количество клиентов: {len(to_read) + len(to_write) - 1}')
print(f"t|r|w -- {len(tasks)}|{len(to_read)}|{len(to_write)}") # показывает колличество клиентов
ready_to_read, ready_to_write, _ = select(to_read, to_write, [])
# print(ready_to_read,ready_to_write)
for sock in ready_to_read:
tasks.append(to_read.pop(sock))
# print(tasks)
for sock in ready_to_write:
tasks.append(to_write.pop(sock))
try:
task = tasks.pop(0)
reason, sock = next(task)
if reason == 'read':
to_read[sock] = task
if reason == 'write':
to_write[sock] = task
except StopIteration:
# print("I'm died again!")
pass
if __name__ == '__main__':
try:
print("------ Loading the IP configuration ------\n"
f"Server ip: {HOST}\n" # Hamachi IP
# "Lockal ip: {}\n"
"------------ Connection start ------------")
# print(HOST)
checkDES_Key()
checkRSA_PrivateKey() # проверка на наличие ключей
tasks.append(server())
event_loop()
except WrongDES_Key as exDES:
print(exDES.message)
pause()
except WrongRSA_Key as exRSA:
print(exRSA.message)
pause()