-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
42 lines (37 loc) · 1.31 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
import socket
from ProtocolImplementation.ClientProtocolImplementation.ClientProtocol import CODEBYTES, FileHandler, ProtocolHandler, StatusHandler
class UserHandler:
def __init__(self, s: socket):
self.s = s
def start(self):
status_handler = StatusHandler(self.s)
file_handler = FileHandler(self.s)
protocol_handler = ProtocolHandler(self.s)
while True:
data = self.s.recv(CODEBYTES)
if status_handler.is_code("Input", data):
protocol_handler.input()
elif status_handler.is_code("Output", data):
protocol_handler.output()
elif status_handler.is_code("GetBytes", data):
file_handler.upload()
elif status_handler.is_code("SendBytes", data):
file_handler.download()
def main():
try:
ip, port = input("Ip address: "), input("Port: ")
except KeyboardInterrupt:
print("\n\nExiting...")
exit()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((ip, int(port)))
print(f"Connected to {s.getsockname()}")
user_handler = UserHandler(s)
user_handler.start()
except KeyboardInterrupt:
print("\n\nExiting...")
finally:
s.close()
if __name__ == "__main__":
main()