SHARE
TWEET

Untitled

kalpin Jun 1st, 2020 725 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Server Code
  2. import socket
  3.  
  4. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. server_socket.bind(("0.0.0.0", 8081))
  6. server_socket.listen()
  7. print("Waiting for connection")
  8. connection_socket, address = server_socket.accept()
  9. print("Client connected\n")
  10.  
  11. #send message
  12. message = "Hello, thanks for connecting"
  13. data = message.encode()
  14. connection_socket.send(data)
  15.  
  16. #receive response
  17. data = connection_socket.recv(1024)
  18. message = data.decode()
  19. print(message)
  20.  
  21. #close connections
  22. connection_socket.close()
  23. server_socket.close()
  24.  
  25. #Client Code
  26. import socket
  27.  
  28. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  29. client_socket.connect(("127.0.0.1", 8081))
  30. print("Connected\n")
  31.  
  32. #get message
  33. data = client_socket.recv(1024)
  34. message = data.decode()
  35. print(message)
  36.  
  37. #respond
  38. message = "Message received"
  39. data = message.encode()
  40. client_socket.send(data)
  41.  
  42. #close connection
  43. client_socket.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top