Guest User

Untitled

a guest
Feb 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.98 KB | None | 0 0
  1. import socket
  2.  
  3. #Set up the socket to accept incoming conections on localhost:8000
  4. server_socket = socket.socket()
  5. server_socket.bind(('127.0.0.1', 8000))
  6. server_socket.listen(1)
  7.  
  8. #Accept a new connection
  9. client_socket, address = server_socket.accept()
  10.  
  11. #Set up variables to use inside the loop
  12. receiving_length = True
  13. str_length = None
  14.  
  15. #Infinite loop to continue receiving data
  16. while True:
  17. if receiving_length:
  18. data = client_socket.recv(2) #The string length is 2 bytes
  19. str_length = int.from_bytes(data, byteorder='little')
  20. receiving_length = not receiving_length #Flip the receiving_length boolean
  21. else:
  22. data = client_socket.recv(str_length)
  23. # print(data.decode('utf-8'))
  24. string = data.decode('utf-8')
  25. receiving_length = not receiving_length #Flip the receiving_length boolean
  26.  
  27. #Check if the string received is the EOT string. If it is, break out of the loop
  28. if string == 'end':
  29. break
  30. print(string)
  31.  
  32. #Close the connection
  33. client_socket.close()
Add Comment
Please, Sign In to add comment