-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
50 lines (41 loc) · 1.94 KB
/
server.py
File metadata and controls
50 lines (41 loc) · 1.94 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
# Программа сервера
from socket import *
import sys
from jim.utils import encode, decode
from jim.config import *
def presence_response(presence_message):
if ACTION in presence_message and \
presence_message[ACTION] == PRESENCE and \
TIME in presence_message and \
isinstance(presence_message[TIME], float):
return {RESPONSE: 200}
else:
return {RESPONSE: 400, ERROR: 'Не верный запрос '}
if __name__ == '__main__':
server = socket(AF_INET, SOCK_STREAM) # Создает сокет TCP
# Получаем аргументы скрипта
try:
addr = sys.argv[1]
except IndexError:
addr = ''
try:
port = int(sys.argv[2])
except IndexError:
port = 7777
except ValueError:
print('Порт должен быть целым числом')
sys.exit(0)
server.bind((addr, port)) # присваеиваем порт 7777
server.listen(5)
while True:
client, addr = server.accept() # Принять запрос на соединение
#print(type(client))
print("Получен запрос на соединение от %s" % str(addr))
data = client.recv(1024) # Получаем запрос от клиента
print("Получен запрос от клиента:", decode(data))
# Подготовим ответ клиенту
presence = encode(presence_response(decode(data))) # переводим словарь в байты
client.send(presence) # Отправляем сообщение слиенту
print("Отправлено сообщение клиенту", presence)
# Закрываем соединение с клиентом
client.close()