transmit an array via socket

Discussion in 'Python' started by Jeff Pang, Oct 27, 2007.

  1. Jeff Pang

    Jeff Pang Guest

    Jeff Pang, Oct 27, 2007
    #1
    1. Advertising

  2. Jeff Pang schrieb:
    > 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
    Diez B. Roggisch, Oct 27, 2007
    #2
    1. Advertising

  3. Jeff Pang

    7stud Guest

    On Oct 26, 11:52 pm, "Jeff Pang" <> wrote:
    > 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
    7stud, Oct 27, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rasha
    Replies:
    0
    Views:
    1,696
    Rasha
    Mar 17, 2005
  2. SteveR
    Replies:
    1
    Views:
    591
    bruce barker
    Oct 19, 2004
  3. Replies:
    6
    Views:
    5,834
    JohnH
    May 10, 2006
  4. Liu Ju
    Replies:
    3
    Views:
    1,360
    Thomas Matthews
    Sep 14, 2004
  5. Hendrik van Rooyen

    Re: transmit an array via socket

    Hendrik van Rooyen, Oct 27, 2007, in forum: Python
    Replies:
    1
    Views:
    306
    Bryan Olson
    Oct 27, 2007
Loading...

Share This Page