На сервере import socket
server_socket = socket.socket()
server_socket.bind(("localhost", 12345))
server_socket.listen(1)
print("Waiting for client to connect...")
client_socket, address = server_socket.accept()
print("Client connected from", address)
# send data to client
client_socket.send(b"Hello from server!")
# receive data from client
data = client_socket.recv(1024)
print("Received data from client:", data.decode())
# close connection
client_socket.close()
У клиента
import socket
client_socket = socket.socket()
client_socket.connect(("localhost", 12345))
# receive data from server
data = client_socket.recv(1024)
print("Received data from server:", data.decode())
# send data to server
client_socket.send(b"Hello from client!")
# close connection
client_socket.close()