Discussion:
[Tutor] FTP from mainframe
Steve Bricker
2010-07-29 16:34:11 UTC
Permalink
BODY { font-family:Arial, Helvetica, sans-serif;font-size:12px;
}This is my first attempt to FTP a file from a mainframe. The code:
import ftplib
session = ftplib.FTP('company.lan.com','userid','passwd')
myfile = open('PC.filename','w')
session.retrlines("RETR 'mainframe.filename'", myfile)
myfile.close()
session.quit()
The resulting error is:
Traceback (most recent call last):
File "ftp_from_mf.py", line 5, in
session.retrlines("RETR 'mainframe.filename'", myfile)
File "c:python26libftplib.py", line 428, in retrlines
callback(line)
TypeError: 'file' object is not callable

Not sure what I'm missing.
Steve Bricker
bob gailer
2010-07-29 17:34:18 UTC
Permalink
Post by Steve Bricker
import ftplib
session = ftplib.FTP('company.lan.com','userid','passwd')
myfile = open('PC.filename','w')
session.retrlines("RETR 'mainframe.filename'", myfile)
myfile.close()
session.quit()
File "ftp_from_mf.py", line 5, in <module>
session.retrlines("RETR 'mainframe.filename'", myfile)
File "c:\python26\lib\ftplib.py", line 428, in retrlines
callback(line)
TypeError: 'file' object is not callable
According to the ftplib module documentation:

retrlines(command[, callback])
Retrieve a file or directory listing in ASCII transfer mode. command
should be an appropriate RETR command (see retrbinary()) or a command
such as LIST, NLST or MLSD (usually just the string 'LIST'). The
callback function is called for each line, with the trailing CRLF
stripped. The default callback prints the line to sys.stdout.

IOW callback must be a function. You are passing a file object. I will
guess that you want:

def writer(line):
myfile.write(line + '\n')

session.retrlines("RETR 'mainframe.filename'", writer)

The documentation is in error in that it does not explicitly state that
a line is passed to the callback function as an argument. I am assuming
that is the case.
--
Bob Gailer
919-636-4239
Chapel Hill NC
Bill Campbell
2010-07-29 18:15:23 UTC
Permalink
Post by Steve Bricker
import ftplib
The easiest way I've found to get a file via ftp in python is to
user urllib, not ftplib. Something like this (add error checking).

import urllib, os
fname='something.pdf'
url = 'ftp://%s@%s:%s/%s' % ('john', 'secret', 'example.com', fname)
# f will have a temporary file name, and h the headers
(f, h) = urllib.urlretrieve(url)
os.rename(f, fname)

Of course this works for http by simply changing the url.

Bill
--
INTERNET: ***@celestial.com Bill Campbell; Celestial Software LLC
URL: http://www.celestial.com/ PO Box 820; 6641 E. Mercer Way
Voice: (206) 236-1676 Mercer Island, WA 98040-0820
Fax: (206) 232-9186 Skype: jwccsllc (206) 855-5792

If you want government to intervene domestically, you're a liberal. If you
want government to intervene overseas, you're a conservative. If you want
government to intervene everywhere, you're a moderate. If you don't want
government to intervene anywhere, you're an extremist -- Joseph Sobran
Alan Gauld
2010-07-29 17:35:00 UTC
Permalink
Post by Steve Bricker
}This is my first attempt to FTP a file from a mainframe.
Thats one more than me!
Post by Steve Bricker
session.retrlines("RETR 'mainframe.filename'", myfile)
File "c:python26libftplib.py", line 428, in retrlines
callback(line)
TypeError: 'file' object is not callable
The error says that its expecting a callable and you are
passing a file object. My guess is you need to create a
function that writes to the file and pass that to ftp.
You could use myfile.write maybe - thats what the
documentation does for a binary file...

HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
Christian Witts
2010-07-30 05:53:28 UTC
Permalink
Post by Steve Bricker
import ftplib
session = ftplib.FTP('company.lan.com','userid','passwd')
myfile = open('PC.filename','w')
session.retrlines("RETR 'mainframe.filename'", myfile)
myfile.close()
session.quit()
File "ftp_from_mf.py", line 5, in <module>
session.retrlines("RETR 'mainframe.filename'", myfile)
File "c:\python26\lib\ftplib.py", line 428, in retrlines
callback(line)
TypeError: 'file' object is not callable
Not sure what I'm missing.
Steve Bricker
_______________________________________________
http://mail.python.org/mailman/listinfo/tutor
When I'm retrieving items I use retrbinary for eg.

from ftplib import FTP

ftp = FTP(url, username, password)
for filename in ftp.nlst(''):
ftp.retrbinary('RETR %s' % filename, open(filename, 'wb').write)
ftp.delete(filename)

ftp.close()

I hope that helps.
--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: ***@compuscan.co.za

NOTE: This e-mail (including attachments )is subject to the disclaimer published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from ***@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6
Alan Gauld
2010-07-30 06:35:31 UTC
Permalink
Post by Christian Witts
When I'm retrieving items I use retrbinary for eg.
The only issue with that is that if this is a real big-iron mainframe
then ftp can translate EBCDIC to ASCII during the transfer whereas
binary will, I think, bring the original file across untranslated.
So you would have the extra decoding step to do manually.
But binary transfer does simplify the transfer process.

But if its not an EBCDIC machine then I'd definitely consider
binary transfer for ftp.
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
Loading...