-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathecho_client.py
More file actions
executable file
·23 lines (19 loc) · 910 Bytes
/
Copy pathecho_client.py
File metadata and controls
executable file
·23 lines (19 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#! /usr/bin/env python3
# A simple tcp client to talk to a echo server
import argparse
import socket
def run_client(host, port):
""" Connect to the server and exchange the messages """
with socket.create_connection((host, port)) as sock:
print(f'Connected from {sock.getsockname()} to {sock.getpeername()},'
f'timeout={sock.gettimeout()},'
f'defaulttimeout={socket.getdefaulttimeout()}')
sock.send(b'Hello, there')
msgfromserver = sock.recv(1024).decode('utf-8')
print(f'msg from server: {msgfromserver}')
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--addr", help="host address", type=str, default="localhost")
parser.add_argument("-p", "--port", type=int, help="server port number: defaults to 12345", default=12345)
args = parser.parse_args()
run_client(args.addr, args.port)