socket receive file does not match sent file

2005-11-06 Thread [EMAIL PROTECTED]
I wrote two simple socket program.
one for sending a file and the other for receiving the file.
but when I run it, a curious thing happened.
The received file was samller that the sent file.

$ cat receivefile.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import socket
import time
import string
import sys

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('', 9000))
sock.listen(5)

try:
   while True:
   # 1. client connect, open file
   newSocket, address = sock.accept()
   print "Connected from ", address

   filename = sys.argv[1]
   f=open(  filename, 'wb')

   # 2. recieve data
   while True:
   data = newSocket.recv(8192)
   if not data: break
   f.write(data)
   # 3.close file
   f.close()
   print filename, "Received\n"

finally:
   sock.close()



$ cat putfile.py
#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import sys
import socket
import string

# "Usage: prog file hostip"

filename   =  sys.argv[1]
host = sys.argv[2]


f=open(filename, 'rb')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, 9000))

while True:
   data = f.read(8192)
   if not data: break
   else:
   s.send(data)
f.close()
s.close()
print filename, " Sent!\n"

$  python receivefile.py  save_apache_1.33.tar.gz
$ python putfile.py apache_1.33.tar.gz localhost

The result is : save_apache_1.33.tar.gz 's size was smaller then the
apache_1.33.tar.gz file

What is the cause of the problem, can anyone tell me?
Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket receive file does not match sent file

2005-11-06 Thread Jean-Paul Calderone
On 6 Nov 2005 09:13:03 -0800, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote:
>I wrote two simple socket program.
>one for sending a file and the other for receiving the file.
>but when I run it, a curious thing happened.
>The received file was samller that the sent file.

Your sender does not take care to ensure the entire file is sent.  It will 
semi-randomly drop bytes from various areas in the middle of the file.  Here's 
a sender that works correctly:

  from twisted.internet import reactor, protocol
  from twisted.protocols import basic
  from twisted.python import log

  filename = sys.argv[1]
  host = sys.argv[2]

  class Putter(protocol.Protocol):
  def connectionMade(self):
  fs = basic.FileSender()
  d = fs.beginFileTransfer(file(filename, 'b'), self.transport)
  d.addCallback(self.finishedTransfer)
  d.addErrback(self.transferFailed)

  def finishedTransfer(self, result):
  self.transport.loseConnection()

  def transferFailed(self, err):
  print 'Transfer failed'
  err.printTraceback()
  self.transport.loseConnection()

  def connectionLost(self, reason):
  reactor.stop()

  f = protocol.ClientFactory()
  f.protocol = Putter
  reactor.connectTCP(host, 9000, f)
  reactor.run()

Of course, this is still not entirely correct, since the protocol specified by 
your code provides not mechanism for the receiving side to determine whether 
the connection was dropped because the file was fully transferred or because of 
some transient network problem or other error.  One solution to this is to 
prefix the file's contents with its length.

Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket receive file does not match sent file

2005-11-06 Thread [EMAIL PROTECTED]
when I test the two program in the same OS,
 i mean from a  redhat 9 OS to a redhat 9 OS,
It's ok. receivefile match sent file.


But when I run receiver on a Redhat 9,
and send file from a windows XP,
the received file's size is randomized.

May be that's where the problem is.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket receive file does not match sent file

2005-11-07 Thread Irmen de Jong
[EMAIL PROTECTED] wrote:
> when I test the two program in the same OS,
>  i mean from a  redhat 9 OS to a redhat 9 OS,
> It's ok. receivefile match sent file.
> 
> 
> But when I run receiver on a Redhat 9,
> and send file from a windows XP,
> the received file's size is randomized.
> 
> May be that's where the problem is.
> 

No. Read Jean-Paul's reply.

--Irmen
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket receive file does not match sent file

2005-11-08 Thread Fredrik Lundh
[EMAIL PROTECTED] wrote:

> while True:
>data = f.read(8192)
>if not data: break
>else:
>s.send(data)

> What is the cause of the problem, can anyone tell me?

using sendall instead of send should fix this.  see the library reference for
details:

send( string[, flags])
  Send data to the socket. The socket must be connected to a remote
  socket. The optional flags argument has the same meaning as for recv()
  above. Returns the number of bytes sent. Applications are responsible
  for checking that all data has been sent; if only some of the data was
  transmitted, the application needs to attempt delivery of the
  remaining data.

  sendall( string[, flags])

  Send data to the socket. The socket must be connected to a remote
  socket. The optional flags argument has the same meaning as for recv()
  above. Unlike send(), this method continues to send data from string
  until either all data has been sent or an error occurs. None is
  returned on success. On error, an exception is raised, and there is no
  way to determine how much data, if any, was successfully sent.

  



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket receive file does not match sent file

2005-11-08 Thread [EMAIL PROTECTED]
Thanks Fredrik Lundh,
 This is great!

I've rewrote the code and it works!
Thanks a lot.

-- 
http://mail.python.org/mailman/listinfo/python-list