-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCPClient.py
More file actions
26 lines (23 loc) · 1.04 KB
/
TCPClient.py
File metadata and controls
26 lines (23 loc) · 1.04 KB
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
import argparse
import socket
import sys
parser = argparse.ArgumentParser(prog='TCPClient',
description='Send data to host using TCP protocol.',
epilog='Example: TCPClient 192.168.1.1 111 "Hello there!"')
parser.add_argument('ip', type=str, help='The IPv4 address to send the data to.')
parser.add_argument('port', type=int, help='The port number to send the data to.')
parser.add_argument('data', type=str, help='The data to send.')
args = parser.parse_args()
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client:
try:
print(f'[*] Connecting to {args.ip}:{args.port}.')
client.connect((args.ip, args.port))
except Exception as ex:
print(f'[!] Unable to connect to {args.ip}:{args.port}.')
print(f'[!] Error: {ex}.')
sys.exit()
dataAsBytes = bytes(map(ord, args.data))
print(f'[*] Sending data.')
client.send(dataAsBytes)
response = client.recv(4096)
print(f'[*] Received response from server: {response.decode()}.')