Guest User

Untitled

a guest
Feb 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import socket
  2.  
  3. #First, we need to add the string 'end' to our arr as the very last element
  4. arr = ['hello', 'strings', 'that', 'need', 'to', 'be', 'transferred', 'across', 'the', 'network', 'using', 'sockets', 'end']
  5.  
  6. #Next, we need to set up the client socket connection
  7. client_socket = socket.socket()
  8. client_socket.connect(('127.0.0.1', 8000))
  9.  
  10. #Next, we loop over the arr, figure out the length of each string, encode that in binary format and send it across.
  11. #Subsequently, we do the same for the actual string
  12. for string in arr:
  13. len_of_string = len(string.encode('utf-8'))
  14. len_in_bytes = (len_of_string).to_bytes(2, byteorder='little')
  15. client_socket.send(len_in_bytes)
  16. client_socket.send(string.encode('utf-8'))
  17.  
  18. #Finally, we close the socket
  19. client_socket.close()
Add Comment
Please, Sign In to add comment