On Oct 26, 11:52 pm, "Jeff Pang" <pa...@juno.comwrote:
I want to transmit an array via socket from a host to another.
How to do it? thank you.
Try this:
client:
-------
import socket
s = socket.socket()
host = 'localhost'
port = 3030
s.connect( (host, port) )
arr = [1, 2, 3]
for elmt in arr:
send_str = "%s," % str(elmt)
while send_str:
chars_sent = s.send(send_str)
send_str = send_str[chars_sent:]
s.close()
server:
-------
import socket
s = socket.socket()
host = "localhost"
port = 3030
s.bind((host, port))
s.listen(5)
while("Ctrl-C hasn't been entered"):
new_sock, addr = s.accept()
data_list = []
while True:
partial_data = new_sock.recv(1012)
data_list.append(partial_data)
if not partial_data: #then got all the data
break
data_str = ''.join(data_list)[:-1] #chop off trailing comma
arr_strs = data_str.split(",")
arr_ints = [int(elmt) for elmt in arr_strs]
print arr_ints