[Tutor] RE: Ftp question

Remco Gerlich scarblac@pino.selwerd.nl
Mon, 14 May 2001 15:11:07 +0200


On  0, Sharriff Aina <NHYTRO@compuserve.com> wrote:
> Remco Gehrlich wrote:
> >It shouldn't do that, as far as I know. It would help if you could post a
> >snippet of your code. Can you test the program from the command line
> instead
> >of in CGI? Does it make a difference? (that's just blind guesswork)
> 
> Yes, actually I havent tried running it as a CGI script at all:
> 
> ###  Pythonwin session ###
> >>> ftp = FTP('zeus')
> >>> ftp.login('sharriff', 'golem'
> '230 User sharriff logged-in'
> >>> ftp.storbinary('stor page2b.jpg', open('c:\\page2b.jpg', 'rb'), 8192)
> '226 Closing data connection'

Actually, this doesn't close your ftp connection, does it? It only closes
the data connection that was setup for that file. It should be possible to
do another storbinary command immediately.

I don't understand why it doesn't work, it works fine here :(.

> this stores an unreadable JPG file on my test server, even with 'rb'

Weird weird weird. Are you sure the original is still good? Are the file
sizes different?

> heres the code that I plan to use in my CGI script:
> 
> ### code snippet from CGI script
> #
> # Iterate thru imageurls,use 'image'( single url) to upload images to
> server
> for each imageurl in imageurls:
> ....tempimagename = os.path.split(imageurl)
> ....imagename = tempimagename[1]    
> ....ftp = FTP(serverip) # connect, serverip is the host name extracted from
> database
> ....ftplogin(usernamevalue, userpasswordvalue)

forgot the .

> ....#ftp.connect(usernamevalue, userpasswordvalue)# crashes ??
> ....ftp.strorbinary("stor %s" %(imagename), open(imageurl, 'rb'), 1024)
> ftp.quit

Don't forget the ()!

> ftp.close()
> # -----------------------------
> ######## CGI script snippet end ###

Something like that should work fine, I would do, I think

import ftplib, os
ftp = ftplib.FTP(serverip)
ftp.login(username, password)
for each imagefile in imagefiles: # They're not exactly urls, just filenames
   ftp.storbinary('STOR '+os.path.basename(imagefile), open(imagefile,'rb'),
                  8192)
ftp.quit()

Still need to find out why the file is mangled on the other side.

-- 
Remco Gerlich