transmit an array via socket

D

Diez B. Roggisch

Jeff said:
I want to transmit an array via socket from a host to another.
How to do it? thank you.

Using XMLRPC or Pyro or other RPC mechanisms instead of reinventing a
wheel that is larger than the socket API suggests in the first place.

Diez
 
7

7stud

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top