-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
55 lines (48 loc) · 1.71 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
import socket
from ProtocolImplementation.ClientProtocolImplementation.ClientProtocol import CODEBYTES, FileHandler, ProtocolHandler, \
StatusHandler
import argparse
class UserHandler:
def __init__(self, s: socket.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():
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--address", help="host address", default=None, type=str, dest="address")
parser.add_argument("-p", "--port", help="port number", default=None, type=int, dest="port")
args = parser.parse_args()
ip = args.address
port = args.port
try:
if not ip:
ip = input("Ip address: ")
if not port:
port = 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()