Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- #First, we need to add the string 'end' to our arr as the very last element
- arr = ['hello', 'strings', 'that', 'need', 'to', 'be', 'transferred', 'across', 'the', 'network', 'using', 'sockets', 'end']
- #Next, we need to set up the client socket connection
- client_socket = socket.socket()
- client_socket.connect(('127.0.0.1', 8000))
- #Next, we loop over the arr, figure out the length of each string, encode that in binary format and send it across.
- #Subsequently, we do the same for the actual string
- for string in arr:
- len_of_string = len(string.encode('utf-8'))
- len_in_bytes = (len_of_string).to_bytes(2, byteorder='little')
- client_socket.send(len_in_bytes)
- client_socket.send(string.encode('utf-8'))
- #Finally, we close the socket
- client_socket.close()
Add Comment
Please, Sign In to add comment