Re: Quetion about flags of socket.recv(bufsize, [flags])

Discussion in 'Python' started by Steve Holden, Feb 8, 2009.

  1. Steve Holden

    Steve Holden Guest

    Ken wrote:
    > I want to receive 4 bytes from a connected socket, I code like this:
    >
    > data = sock.recv(4)
    >
    > There is a problem with above code. The recv method will not block until
    > it get all 4 bytes. So I use the second param of recv method like this
    >
    > data = sock.recv(4, socket.MSG_WAITALL)
    >
    > This works fine on linux with python 2.5, while on windows, the
    > interpreter tells me 'AttributeError: 'module' object has no attribute
    > 'MSG_WAITALL''. How can I work this out, or there is another way to get
    > certain bytes from a socket and block if it hasn't got enough bytes?
    >

    If you *need* 4 bytes then the best way to receive them is simply to
    loop until you get them (assuming you are using blocking sockets):

    BLOCKSIZE = 4
    buffer = ""
    while len(buffer) < BLOCKSIZE:
    data = sock.recv(BLOCKSIZE-len(buffer))
    if not data:
    break # other end is closed!
    buffer += data

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Feb 8, 2009
    #1
    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. Thomas =?ISO-8859-15?Q?G=FCttler?=

    Maxmium bufsize using open2?

    Thomas =?ISO-8859-15?Q?G=FCttler?=, Jul 10, 2003, in forum: Python
    Replies:
    2
    Views:
    350
    Piet van Oostrum
    Jul 12, 2003
  2. Jonathan Hudgins

    popen bufsize not allowed on windows

    Jonathan Hudgins, Jul 24, 2004, in forum: Python
    Replies:
    1
    Views:
    363
    Jonathan Hudgins
    Jul 25, 2004
  3. I. Myself
    Replies:
    2
    Views:
    486
    I. Myself
    Apr 30, 2006
  4. =?iso-8859-1?B?aWFuYXLp?=

    real time updating of popen, bufsize=0 problems

    =?iso-8859-1?B?aWFuYXLp?=, Apr 6, 2007, in forum: Python
    Replies:
    4
    Views:
    576
    =?iso-8859-1?B?aWFuYXLp?=
    Apr 6, 2007
  5. deepak nayak
    Replies:
    3
    Views:
    425
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=
    Oct 25, 2007
Loading...

Share This Page