FTP status problems. (Again)

N

Nainto

Hello, I have posted before about trying to find the status of an FTP
uplaod but couldn't get anything to work. After some more searching I
found
http://groups.google.com/group/comp...dc04d4?q=ftp+progress&rnum=1#91917c906cdc04d4
but it does not seem to work because it just uploads the file and does
not print a . onto the screen. HEre is the code I have when I'm using
the code from that link.
import ftplib
import os
class dot_FTP(ftplib.FTP):
def storbinary(self, cmd, fp, blocksize=8192):
self.voidcmd('TYPE I')
conn = self.transfercmd(cmd)
while 1:
buf = fp.read(blocksize)
if not buf: break
conn.send(buf)
sys.stdout.write('.')
sys.stdout.flush()
conn.close()
return self.voidresp()


ftp = ftplib.FTP("FTPADDRESS")
ftp.login("user","pass")
file = "/file"
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
ftp.quit()
Does anyone know why this is not working? IS there any other way to
find out when a chunc has been sent or the bytes uploaded of a file?
Thanks.
 
M

marduk

Hello, I have posted before about trying to find the status of an FTP
uplaod but couldn't get anything to work. After some more searching I
found
http://groups.google.com/group/comp...dc04d4?q=ftp+progress&rnum=1#91917c906cdc04d4
but it does not seem to work because it just uploads the file and does
not print a . onto the screen. HEre is the code I have when I'm using
the code from that link.
import ftplib
import os
class dot_FTP(ftplib.FTP):
def storbinary(self, cmd, fp, blocksize=8192):
self.voidcmd('TYPE I')
conn = self.transfercmd(cmd)
while 1:
buf = fp.read(blocksize)
if not buf: break
conn.send(buf)
sys.stdout.write('.')
sys.stdout.flush()
conn.close()
return self.voidresp()


ftp = ftplib.FTP("FTPADDRESS")
ftp.login("user","pass")
file = "/file"
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
ftp.quit()
Does anyone know why this is not working? IS there any other way to
find out when a chunc has been sent or the bytes uploaded of a file?
Thanks.

.... and I haven't tried this myself, but you should be able to subclass
the builtin file object and prepare your own read() method. Something
like

class ProgressFile(file):

def read(self, size = None):
print '.',

if size is not None:
return file.read(self, size)
else:
return file.read()

May need some tweaking.. then store the file as

ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)

Give it a try..

-m
 
M

marduk

... and I haven't tried this myself, but you should be able to subclass
the builtin file object and prepare your own read() method. Something
like

class ProgressFile(file):

def read(self, size = None):
print '.',

if size is not None:
return file.read(self, size)
else:
return file.read()

May need some tweaking.. then store the file as

ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)

Give it a try..

-m

I corrected some errors and made some modifications to my previous post:

class ProgressFile(file):
def read(self, size = None):
from sys import stdout

if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff


if __name__ == '__main__':
import sys

fname = sys.argv[1]
f = ProgressFile(fname)
f.read()
 
N

Nainto

When I execute the folllowing code with all of the address, username,
and passwords filled out I gt an error saying:
"/Library/Frameworks/Python.framework/Versions/2.4/Resources/Python.app/Contents/MacOS/Python"
"/Users/zacim/Documents/FireUpFTP/foramttedthing" ; exit
Traceback (most recent call last):
File "/Users/zacim/Documents/FireUpFTP/foramttedthing", line 26, in ?
fname = sys.argv[1]
IndexError: list index out of range
logout
[Process completed]

This is the code:

import ftplib
class ProgressFile(file):
def read(self, size = None):
from sys import stdout

if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff
if __name__ == '__main__':
import sys
fname = sys.argv[1]
f = ProgressFile(fname)
f.read()

ftp = ftplib.FTP("ftp.sadpanda.cjb.cc")
ftp.login("sadpanda","s4dp4nd4b1g")
file = "/FrenchBrochure.pages.zip.gz"
ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
ftp.quit()
 
K

Kartic

That is because you have just taken marduk's program and included your
ftp code... please see what he has done in the if __name__ == '__main__'
part.

He expects the file name as an commandline argument (sys.argv[1]) and
since you just copied his code, you are invoking the script *without*
the argument. This is what the exception means when it says "list index
out of range".

To make ProgressFile class work for you with the filename you have
hardcoded... make the following modification to the "if __name ==" part:

if __name__ == '__main__':
ftp = ftplib.FTP("ftp.sadpanda.cjb.cc")
ftp.login("sadpanda","s4dp4nd4b1g")
file = "/FrenchBrochure.pages.zip.gz"
ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
ftp.quit()

Thanks,
-Kartic

The Great 'Nainto' uttered these words on 9/17/2005 9:08 AM:
 
N

Nainto

I'm really sorry that I keep having problems with this. :-( Now I get:
TypeError: Error when calling the metaclass bases[] str() takes at most
1 arguement (3 given)

and the Traceback is:

file "formattedthing", line 2, in '?'
classProgressFile(file)

With the following code:

import ftplib
class ProgressFile(file):
def read(self, size = None):
from sys import stdout
if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff
if __name__ == '__main__':
ftp = ftplib.FTP("ftp.sadpanda.cjb.cc")
ftp.login("sadpanda","PASSWORDEDITEDOUT")
file = "/FrenchBrochure.pages.zip"
ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)
ftp.quit()

Thanks so muchy for help guys. :)
 
K

Kartic

Hello,
> file = "/FrenchBrochure.pages.zip"
> ftp.storbinary("STOR " + file, ProgressFile(file, "rb"), 1024)

You are using file = "/FrenchBrochure.pages.zip" (sorry I did not notice
earlier).

You can not use file as variable name like you have, as it represents a
file object in python.

Please change your variable name from file to filename.

Thanks.
 
N

Nainto

Unfortunatly I stiill get the same error. :-(
Here is the code:
import ftplib
class ProgressFile(file):
def read(self, size = None):
from sys import stdout
if size is not None:
buff = file.read(self, size)
if buff:
stdout.write('.')
else:
stdout.write('\n')
return buff
else:
buff = ''
while True:
new_str = file.read(self, 1024)
stdout.write('.')
if new_str:
buff = buff + new_str
else:
stdout.write('\n')
break
return buff
if __name__ == '__main__':
ftp = ftplib.FTP("ftp.sadpanda.cjb.cc")
ftp.login("sadpanda","PASSWORDEDITIEDOUT")
filename = "/FrenchBrochure.pages.zip"
ftp.storbinary("STOR " + filename, ProgressFile(filename, "rb"), 1024)

ftp.quit()


Thanks again.
 
K

Kartic

Unfortunatly I stiill get the same error. :-(

The same "metaclass bases[]" error??
Here is the code:
import ftplib -snip-
if __name__ == '__main__':
ftp = ftplib.FTP("ftp.sadpanda.cjb.cc")
ftp.login("sadpanda","PASSWORDEDITIEDOUT")
filename = "/FrenchBrochure.pages.zip"
ftp.storbinary("STOR " + filename, ProgressFile(filename, "rb"), 1024)

ftp.quit()


Your code works for me.. I just made one minor change.

I gave a different target name:

filename = "/FrenchBrochure.pages.zip"
target = "FrenchBrochure.pages.zip"
ftp.storbinary("STOR " + target, ProgressFile(filename, "rb"), 1024)

Thanks,
-Kartic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,078
Latest member
MakersCBDBlood

Latest Threads

Top