socket receive file does not match sent file
zelzel.zsu at gmail.com
zelzel.zsu at gmail.com
Sun Nov 6 12:13:03 EST 2005
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.
More information about the Python-list
mailing list