#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    Sending arrays over sockets


    I was wondering if there is an easy way to send an array over a socket that is holding integers? THis array can be up to 4 mbs in size so it is pretty large.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You can only send a string over a socket; or at least that is how Python sees it. However you could send each element in the array and add it to a list at the other end .

    One alternative to this would be to send the string representation of the list and parse it at the other end to bring it back to life; you could use eval() to do this although for safety this probably isn't the best idea IMO.

    Hope this helps,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Location
    Regensburg, Germany
    Posts
    147
    Rep Power
    16
    There is a general method for sending python objects over arbitrary channels using the pickle module. With the pickle module you can translate objects into binary or ascii data streams and vice versa. For an array this may look like this (copied from an IDLE session):
    Code:
    >>> 
    >>> import pickle
    >>> pickle.dumps([1, 2, 3, 4])
    '(lp0\nI1\naI2\naI3\naI4\na.'
    >>> pickle.loads('(lp0\nI1\naI2\naI3\naI4\na.')
    [1, 2, 3, 4]
    >>>
    Note that using pickle may rise security issues like using eval or exec as netytan pointed out before.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2004
    Location
    Albuquerque, New Mexico
    Posts
    137
    Rep Power
    11
    You could try something like Pickle, if you dont mind the security issues. Or, you could use something like zlib and then send it over the socket.

IMN logo majestic logo threadwatch logo seochat tools logo