create a tcp client in python

Here is an example code for creating a TCP client in Python:

main.py
import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024
MESSAGE = "Hello, World!"

# create a TCP/IP socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect the socket to the server's address and port
s.connect((TCP_IP, TCP_PORT))

# send the message to the server
s.send(MESSAGE.encode())

# receive the data from the server
data = s.recv(BUFFER_SIZE)

# close the socket
s.close()

print("Received data:", data.decode())
454 chars
24 lines

In this example, the client connects to a server running on 127.0.0.1 (localhost) on port 5005. It then sends the message "Hello, World!" to the server and waits for a response. Once the response is received, it prints it out to the console and closes the connection.

gistlibby LogSnag