-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
executable file
·82 lines (59 loc) · 2.28 KB
/
client.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
"""Client"""
#!/usr/bin/python3
import socket
import json
class Client:
"""Class client"""
def __init__(self, host ='127.0.0.1', port = 65432):
self.user_name = ''
self.connected_with_serwer = False
self.client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.connect(host, port)
self.command()
except socket.error:
print('Server error')
finally:
self.disconnect()
def connect(self, host:str, port:int)-> None:
"""Function connect to serwer"""
self.client.connect((host,port))
self.connected_with_serwer = True
print('Connected to the server')
def disconnect(self)->None:
"""Function disconnect to the serwer"""
print('Closing connection to the server')
self.client.close()
def command(self)-> None:
"""Function of sending and receiving commands"""
while self.connected_with_serwer:
self.login_user()
while not self.user_name == '':
command_to_send = input('command: ')
self.send_command(command_to_send)
if command_to_send == 'stop':
return
else:
self.recv_command()
if command_to_send == 'logout':
self.user_name = ''
def login_user(self)->bool:
"""Function login user in server"""
while True:
print('Please log in')
self.send_command({\
'user' : input('Login: '), \
'password': input('Password: ')})
answer_from_server = json.loads(self.client.recv(2048).decode())
if answer_from_server['answer'] == 'ok':
self.user_name = answer_from_server['user']
print('Logged in')
return True
print('Wrong login or password')
def send_command(self, command:str):
"""Function send command to server"""
self.client.send(json.dumps({'user': self.user_name, 'command': command}).encode())
def recv_command(self):
"""Function receiving from server"""
print('Server: ' + json.loads(self.client.recv(2048).decode())['command'])
client1 = Client()