fork download
  1. import socket
  2.  
  3. host = socket.gethostname() # Or the server's IP address if not on the same machine
  4. port = 1223
  5.  
  6. try:
  7. cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Create socket
  8. cs.connect((host, port)) # Connect to the server
  9. print("Socket created successfully")
  10.  
  11. while True:
  12. msg1 = input("Enter message to send: ")
  13. s = msg1.encode() # Encode the message to bytes
  14. cs.send(s) # Send the message
  15. s1 = cs.recv(2048) # Receive the response (up to 2048 bytes)
  16. s2 = s1.decode() # Decode the response to a string
  17. print("Reversed string from server is: %s" % s2)
  18.  
  19. if msg1.lower() == "exit":
  20. break
  21.  
  22. cs.close() # Close the socket
  23.  
  24. except socket.error as msg1:
  25. print("Unable to connect server with error %s" % msg1)
  26.  
Success #stdin #stdout 0.02s 9576KB
stdin
Standard input is empty
stdout
Unable to connect server with error [Errno -3] Temporary failure in name resolution