Re: Read / Write image file

Discussion in 'Python' started by phil, May 3, 2005.

  1. phil

    phil Guest

    >
    > By the way, what is 'rb' and 'wb' ?
    >


    Read Binary, Write Binary
    In Win32 the default is text mode which
    will screw up binary files

    > Also, when I create a client/server sockets I do something like...
    >
    > SERVER
    > -----------
    > server.bind(('', 4321))
    > (sock, addr) = server.accept()
    > x = server.recv(1024)
    >
    > CLIENT
    > ------------
    > client.connect(('localhost', 4321))
    > x = open("abc.txt", "rb")
    > client.send(x)
    > client.close()
    > x.close()
    >
    > ...when I do this I get a constant beeping on my PC, any idea why?
    > Also, on the server ...how can I set that up so I can receive a
    > file/data of unknown size instead of just 1024?
    >


    1024 is just the buffer size. You need to recv in a loop
    until the len(x) is 0. You need to do a little reading on sockets.

    Beeping I've no idea. Perhaps packets not handled????
    phil, May 3, 2005
    #1
    1. Advertising

  2. phil

    codecraig Guest

    so something like,

    x = sock.recv(1024)
    while (len(x) > 0):
    # do stuff
    x = sock.recv(1024)


    ??

    So what if the client sends 4 bytes, and then sends 8000 bytes? WIll I
    get the first 4 bytes as a separate msg so to speak? Basically i want
    to catch each message from the client as a whole.
    codecraig, May 4, 2005
    #2
    1. Advertising

  3. phil

    Peter Hansen Guest

    codecraig wrote:
    > so something like,
    >
    > x = sock.recv(1024)
    > while (len(x) > 0):
    > # do stuff
    > x = sock.recv(1024)
    >
    >
    > ??
    >
    > So what if the client sends 4 bytes, and then sends 8000 bytes? WIll I
    > get the first 4 bytes as a separate msg so to speak? Basically i want
    > to catch each message from the client as a whole.


    You cannot guarante *anything* except that (I believe) you will not
    receive zero bytes from .recv() as long as the socket is open.

    Those four bytes the client sends: technically they could come as any
    combination, including four separate one-byte packets (though that is at
    least highly unlikely, if not actually impossible in practice). The
    8000 bytes? Some of them could actually arrive with the four bytes, and
    they could also be split into many smaller packets, with you having
    absolutely no say in the matter, and no way to control it.

    Don't fight the way TCP works: learn to use it the way everyone else
    does, or better yet: don't reinvent the wheel. Use an existing package
    that already does all the low-level stuff that you are trying to do and
    which does it reliably. Twisted, some of the standard library options,
    or something else. At the very least, look at some existing code to see
    how complex it has to be to work reliably, or look at a book like the
    Nutshell book to learn to do it properly.

    -Peter
    Peter Hansen, May 4, 2005
    #3
  4. phil wrote:
    >>
    >> By the way, what is 'rb' and 'wb' ?
    >>

    >
    > Read Binary, Write Binary
    > In Win32 the default is text mode which
    > will screw up binary files
    >

    In fact, this is not just for Windows. Linux / Unix is unusual
    using the originally non-standards-conforming line feed ('\n')
    character as a line separator. Various different OS's define
    text files in different ways. The distinction between binary
    and text is a C standards thing, not a Windows goof. Heaven
    knows I dislike a lot of what Microsoft has done in the way
    of interfering with standard ways of doing things, but their
    end-of-line convention is not one of those things. The use
    of the extension ".doc" was such a deliberate violation of
    standards. Note older Apple files used '\r' as a line separator,
    Tenex used \r\n, or \037, some systems used a line count (leaving
    no end-of-line string), and so on....

    It is a good idea to provide an indication of whether a file
    is binary or text to the file system; the clue could guide
    compression information.

    -Scott David Daniels
    Scott David Daniels, May 4, 2005
    #4
  5. phil

    phil Guest

    Re: Read / Write image file REC SEP

    >>
    >>So what if the client sends 4 bytes, and then sends 8000 bytes? WIll I
    >>get the first 4 bytes as a separate msg so to speak? Basically i want
    >>to catch each message from the client as a whole.
    >>


    IF YOU WANT SEPARATE MESSAGES:
    Modify the client to send an end of record, then:
    On Server:


    datasep = '<EOR>' # or whatever you like
    datal = []
    seversocket.settimeout(5.0) # or 0(infinite) or something reasonable
    while 1:
    try:
    data = serversocket.recv(BUFFERSIZE) # fails on timeout
    if not data: break
    datal.append(data)
    except: break

    msglist = datal.split(datasep) # a list of messages
    phil, May 4, 2005
    #5
    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. JL
    Replies:
    2
    Views:
    94
    Tim Chase
    Dec 14, 2013
  2. Cameron Simpson
    Replies:
    0
    Views:
    85
    Cameron Simpson
    Dec 15, 2013
  3. Chris Angelico
    Replies:
    0
    Views:
    95
    Chris Angelico
    Dec 15, 2013
  4. Tim Chase
    Replies:
    0
    Views:
    87
    Tim Chase
    Dec 16, 2013
  5. Cameron Simpson
    Replies:
    0
    Views:
    74
    Cameron Simpson
    Dec 18, 2013
Loading...

Share This Page