-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive_client.py
executable file
·38 lines (28 loc) · 1.05 KB
/
naive_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
import socket
import logging
import argparse
import sys
def main(host, port):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.connect((host, port))
while True:
command = input(">> ")
s.sendall(command.encode('ascii'))
response = s.recv(1024)
if not response: break
print(response.decode('ascii'), end='')
except socket.timeout as e:
logging.error(e)
except ConnectionAbortedError:
logging.info("Server forced a disconnection")
except KeyboardInterrupt:
logging.info("Goodbye")
s.close()
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
parser = argparse.ArgumentParser()
parser.add_argument("--host", metavar="host", type=str, default='localhost', required=False)
parser.add_argument("--port", metavar="port", type=int, default=9999, required=False)
args = parser.parse_args(sys.argv[1:])
main(args.host, args.port)