You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

20 lines
579 B

  1. import socket
  2. host_ip, server_port = "127.0.0.1", 9999
  3. data = " Hello how are you?\n"
  4. # Initialize a TCP client socket using SOCK_STREAM
  5. tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6. try:
  7. # Establish connection to TCP server and exchange data
  8. tcp_client.connect((host_ip, server_port))
  9. tcp_client.sendall(data.encode())
  10. # Read data from the TCP server and close the connection
  11. received = tcp_client.recv(1024)
  12. finally:
  13. tcp_client.close()
  14. print("Bytes Sent: {}".format(data))
  15. print("Bytes Received: {}".format(received.decode()))