Sockets: Receiving C Struct

Dan Stromberg drsalists at gmail.com
Fri Aug 5 17:09:09 CEST 2011


First, s.recv(4) is not guaranteed to always return 4 bytes.  It could
return 0, 1, 2, 3, or 4, wtih 4 being the most likely.  To deal with this, I
tend to use http://stromberg.dnsalias.org/~dstromberg/bufsock.html - but I
suspect that Twisted has a way of dealing with it too.

Then, to put your data into a list, just create an empty list with [] and
use list_.append(integer).

Or was there more to your question, like perhaps a traceback you haven't
shared with us?

On Fri, Aug 5, 2011 at 6:05 AM, Johnny Venter <Johnny.Venter at zoho.com>wrote:

> I was not sure if this message was sent before my membership was accepted.
>
> Please disregard if it's a duplicate.
>
> Thanks
>
> Begin forwarded message:
>
> *From: *Johnny Venter <Johnny.Venter at zoho.com>
> *Date: *August 5, 2011 8:15:53 AM EDT
> *To: *python-list at python.org
> *Subject: **Sockets: Receiving C Struct*
>
> New to python and would like to test network/sockets with it.
>
> I am having a problem where I have setup the following:
>
> import socket, sys, struct
>
> HOST = '1.1.1.1'
> PORT = 153
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((HOST, PORT))
>
> data = struct.unpack('!I',s.recv(4))[0]
>
> s.close()
>
> print data
>
>
> What I would like to do is take the input from the server, and store it in
> an array.  The type of data is int.
>
> Can someone help?
>
>
> Thanks, Johnny
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110805/904f8981/attachment.html>


More information about the Python-list mailing list