newbie needs a little help... trying to write an ftp script.

G

google account

hiya,

I am a network admin type person who has (after a bit of research and
a couple of dabblings) decided that python is the language for me.

I have read through learning python, nad it has managed to give me a
great basis for the things I have done previously (regex manipulation
of config files, mostly. I got a great application built fairly
quickly that would parse an apache config, add a VirtualHost entry,
sort it alphabetically and sighup the deamon) but my present task is
something that doesn't really get covered in this most excellent tome.


I want to create an ftp script that will (from a windows2000 host) log
into an ftp server, change down one directory, download specific
files from this directory (wildcard passing...?) and then log out.
Once it has completed this task, it should email someone to inform
them of completion status, perhaps including the output of the ftp
session.

I can't imagine that it would be too hard, but I don't see anything
afer a google search that will give me any pointers so I was hoping I
might get some in response to this post. I have found the ftplib,
and the info on the python.org docs. I am needing a pointer on the
syntax, or a bit of a (non-programmer) translation with what they
mean.

The docs suggest that I could do something like....

import ftplib

ftp = ftplib.FTP('172.30.30.30') # This is wrong, somehow...?
ftp.login(user,pass)
ftp.retrlines('LIST') # I believe it might need to be
ftp.retrlines('ls -al')
ftp.sendcmd('prompt')
ftp.cwd('folder')
ftp.retrbinary(retr *tgz)
ftp.quit()



Or that maybe I could define a new class with something like this:

class HandleFTP(ftplib.FTP):
(I am not sure what would follow here, but I'm thinking it might be
what I want. This way I get to define a few functions within my own
class, and these can capture all the server output to a text file,
for later emailling.)



Basically, I am getting errors with the first line somehow, and I am
not sure what to replace it with.

Some of these things, the docs tell me they return a string. Does
that mean I should initialise a string for them? Would they not work
if that isn't happening, or would the output just end up in
/dev/null?



Thanks for looking!

Googleboy
 
D

Diez B. Roggisch

import ftplib
ftp = ftplib.FTP('172.30.30.30') # This is wrong, somehow...?
ftp.login(user,pass)
ftp.retrlines('LIST') # I believe it might need to be
ftp.retrlines('ls -al')
ftp.sendcmd('prompt')
ftp.cwd('folder')
ftp.retrbinary(retr *tgz)
ftp.quit()

Your example worked fine with me for "ftp.suse.com" - at least the first 3
lines. It didn't understand ls -al and prompt, and a folder "folder" didn't
exist of course.

The ftp.retrbinary(retr *tgz) is also flawed - what is retr, a variable? I
think you need to look up that command again. And *tgz can't be right, too.
You most probably want "*tgz" there (a string literal)

ftp.quit() works.

Diez
 
W

Wojtek Walczak

Dnia 28 Jul 2003 11:40:48 -0700, google account napisa³(a):
[...]
The docs suggest that I could do something like....

import ftplib

ftp = ftplib.FTP('172.30.30.30') # This is wrong, somehow...?
ftp.login(user,pass)
^^^^
Do not use pass in this way - it's a python's keyword. Use some other
name instead.
ftp.retrlines('LIST') # I believe it might need to be
ftp.retrlines('ls -al')
ftp.sendcmd('prompt')
ftp.cwd('folder')
ftp.retrbinary(retr *tgz)
ftp.quit()
Here's a simple example:

import ftplib,sys

ftp = ftplib.FTP('ftp.python.org')
ftp.login('anonymous','(e-mail address removed)')
print ftp.retrlines('LIST')
ftp.cwd('pub/python')
print ftp.retrlines('LIST')
print ftp.retrbinary('retr README', sys.stdout.write)
ftp.quit()
 
G

google account

Wojtek Walczak posted a simple example:

import ftplib,sys

ftp = ftplib.FTP('ftp.python.org')
ftp.login('anonymous','(e-mail address removed)')
print ftp.retrlines('LIST')

I think it works the same if I use print or simply
ftp.retrlines('list')

ftp.cwd('pub/python')
print ftp.retrlines('LIST')


I am interested in reading this into a string so that I can process
it. I need to get the name of each file in teh directory so that I
can do a loop to retr them all; ie, strip out all directories, and
then strip out all the extra information (permissions, dates. I try
stuff like

SomeString = ftp.retrlines('list')

but this only seems to read in the return string:

print SomeString
226 Transfer Complete

IS there anyway I can read in the whole thing? It is obviously
taking the string as being the only part between the single quotes...
I don't know how to read the rest into a string.


print ftp.retrbinary('retr README', sys.stdout.write)

This is the line I am most interested in! I assume that the callback
has something to do with how the program handles the data it is
retrieving, and that to save the file it is retrieving I have to open
a file, write the file, then close the file... This line would be
the writing of the file, I am guessing. something like

filename = open(r'd:\downloads\' + file, 'w')
ftp.retrbinary('retr README', filename)
filename.close

And that I can run this thing through a loop for each of the filenames
that I get outta the first string.


Am I completely way offbase here with what I am attempting? Is this
going to actually download the files from the ftp server to the local
hardware? Once I can read the output into a string I guess I can get
kicked off. This is my current hurdle.


Thanks for looking!

googleboy.
 
P

Paolo G. Cantore

Here is a sample session that should explain all your questions (hopefully?)

[paolo@localhost ftp_sess]$ python
Python 2.2 (#4, Jul 3 2003, 21:17:46)
[GCC 2.95.3 19991030 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ftplib
>>> f=ftplib.FTP("localhost")
>>> f.login("paolo","xxxxxxx") '230 User paolo logged in.'
>>> files=f.nlst("*.html")
>>> files ['LDP.html', 'zenirc.html', 'zenirc_toc.html']
>>> for file in files:
.... f.retrbinary("RETR %s" % file, open(file,"wb").write)
....
'226 Transfer complete.'
'226 Transfer complete.'
'226 Transfer complete.'

(if you don't like the completion message).... y=f.retrbinary("RETR %s" % file, open(file,"wb").write)
....
(now as a file like string object)'226 Transfer complete.'
'<HTML>\n<HEAD>\n<!-- Created by texi2html 1.56k from LDP on 29 May 2003
-->\n\n<TITLE>Untitled Document</TITLE>\n</HEAD>\n<BODY>\n<H1>Untitled
Document</H1>\n<P>\n<P><HR><P>\n<P><HR><P>\nThis document was generated
on 29 May 2003 using\n<A

google account wrote:

-------------------------snipped-------------------------
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top