T
T
TODO•SEC
Search…
TcpClient.py
TCP Client script from Python section of PTSv4 (eLearnSecurity)
1
# Goal
2
# Client that starts a connection to the Python server
3
# and sends a message. Use function 'connect'
4
5
# Notes, to start listening on port 1234 in Linux,
6
# use "socat -v tcp-l:1234,fork exec:'/bin/cat'"
7
8
import socket
9
10
CLT_ADDR = input("Server's IP: ")
11
CLT_PORT = int(input("Type the TCP port you would like to connect to: "))
12
13
# Create new socket using the default family socket (AF_INET) that uses TCP
14
# the default socket type connection-oriented (SOCK_STREAM)
15
16
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
17
18
# Connect to a remote socket at address
19
20
s.connect((CLT_ADDR, CLT_PORT))
21
22
# print Target IPv4 address and TCP port
23
24
print("Connected to:", CLT_ADDR, "on port:", CLT_PORT, "\n")
25
26
message = input("Enter message to send: ")
27
28
s.sendall(message.encode())
29
s.close()
Copied!
Last modified 8d ago
Copy link