Advertisement
Guest User

server_socket.py

a guest
Jul 1st, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import socket
  2.  
  3. #create socket - parameters tell type of socket (IP), and protocol (TCP)
  4. server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5. #bind socket to address - all addresses on this computer, on port 8081
  6. #8081 typically used for testing
  7. server_socket.bind(("0.0.0.0", 8081))
  8.  
  9. #set socket to listen
  10. server_socket.listen()
  11. print("waiting for connection")
  12.  
  13. #accept socket - waits, accepts w/ two return
  14. connection_socket, address = server_socket.accept()
  15. print("client connected")
  16.  
  17.  
  18. #added in section 1.8
  19. #sends data
  20. message = "Hello, thanks for connecting"
  21. data = message.encode() #encodes message as a series of bytes
  22. connection_socket.send(data)
  23.  
  24. #challenge at the end of 1.8
  25. #receive and display a response message
  26. data = connection_socket.recv(1024)
  27. response = data.decode()
  28. print(response)
  29.  
  30. #close sockets
  31. connection_socket.close()
  32. server_socket.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement