Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Read / Write image file

Reply
Thread Tools

Re: Read / Write image file

 
 
phil
Guest
Posts: n/a
 
      05-03-2005
>
> 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????

 
Reply With Quote
 
 
 
 
codecraig
Guest
Posts: n/a
 
      05-04-2005
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.

 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      05-04-2005
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
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      05-04-2005
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
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
phil
Guest
Posts: n/a
 
      05-04-2005
>>
>>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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need to write Simple JAVA program to read and write from USB serialto use it with Arduino sahm Java 4 10-15-2012 06:24 PM
File.read(fname) vs. File.read(fname,File.size(fname)) Alex Dowad Ruby 4 05-01-2010 08:20 AM
Read / Write image file codecraig Python 3 05-03-2005 07:23 PM
Read / Write image file phil Python 2 05-03-2005 07:06 PM
Re: How to change Read Only Constraint to Read-Write Isaac VHDL 0 07-10-2003 01:43 PM



Advertisments