Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling ftp commands from python

2 views
Skip to first unread message

Thierry Lam

unread,
Aug 31, 2005, 4:49:43 PM8/31/05
to
Is it possible to run an ftp command to connect to some remote computer
on the network.

For example, if I want to retrieve some data from
\\remcomputer\datafiles on the network and copy it to my local
computer, how do I do it in python on the Unix side?

I don't want to use mount since I don't have permission.

Thanks
Thierry

drag...@gmail.com

unread,
Aug 31, 2005, 4:59:05 PM8/31/05
to

Your best bet would be to use "pexpect" module. Code may look something
like:

import pexpect
import sys
child = pexpect.spawn ('ftp ftp.site.com')
child.expect ('Name .*: ')
child.sendline ('username')
child.expect ('Password:')
child.sendline ('password')
child.expect ('ftp> ')
child.sendline ('cd testdir')
child.expect ('ftp> ')
child.sendline ('bin')
child.expect ('ftp> ')
child.sendline ('hash')
child.expect ('ftp> ')
child.sendline ('get testfile')
child.expect ('ftp> ')
print child.before
child.sendline ('bye')

Raghu.
~

Robert Kern

unread,
Aug 31, 2005, 5:08:43 PM8/31/05
to pytho...@python.org
Thierry Lam wrote:
> Is it possible to run an ftp command to connect to some remote computer
> on the network.

If the remote computer is running an ftp server, yes. If not, no.

> For example, if I want to retrieve some data from
> \\remcomputer\datafiles on the network and copy it to my local
> computer, how do I do it in python on the Unix side?
>
> I don't want to use mount since I don't have permission.

http://miketeo.net/projects/pysmb/

--
Robert Kern
rk...@ucsd.edu

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter

Steve Holden

unread,
Aug 31, 2005, 5:40:48 PM8/31/05
to pytho...@python.org
http://www.holdenweb.com/Python/PDCode/ftpStream.py shows you how to
drive a remote FTP server using an object-oriented approach. Don;t know
whether it'll help.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Cameron Laird

unread,
Aug 31, 2005, 8:08:06 PM8/31/05
to
In article <1125521945....@g47g2000cwa.googlegroups.com>,

... and, for those for whome pexpect is somehow infeasible,
there are a variety of ways to work around its absence <URL:
http://phaseit.net/claird/comp.unix.programmer/ftp_automation.html >.

Maurice LING

unread,
Aug 31, 2005, 8:57:24 PM8/31/05
to

I use ftplib in the standard libraries.

from ftplib import FTP

def grab_geneontology():
"""Function to download gene ontology file."""
ftp = FTP('ftp.geneontology.org')
ftp.login()
ftp.cwd('/pub/go/ontology')
ftp.retrbinary('retr gene_ontology.obo',
open('gdata/gene_ontology', 'wb').write)
ftp.quit()

if __name__ == '__main__': grab_geneontology()

maurice

Mike Meyer

unread,
Aug 31, 2005, 11:10:54 PM8/31/05
to
"Thierry Lam" <lamth...@gmail.com> writes:

> Is it possible to run an ftp command to connect to some remote computer
> on the network.

Yes, but why would you want to do taht?

> For example, if I want to retrieve some data from
> \\remcomputer\datafiles on the network and copy it to my local
> computer, how do I do it in python on the Unix side?

Start with "import ftplib". Use ftplib to read the file, and write it
out wherever you want it - or don't, if the goal was to get the
contents of the file into your Python program. The API is sorta ugly,
but it works, and comes with Python.

<mike
--
Mike Meyer <m...@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

0 new messages