By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,942 Members | 1,788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,942 IT Pros & Developers. It's quick & easy.

Calling ftp commands from python

P: n/a
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

Aug 31 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

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.
~

Aug 31 '05 #2

P: n/a
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

Aug 31 '05 #3

P: n/a
Thierry Lam wrote:
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

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/

Aug 31 '05 #4

P: n/a
In article <11*********************@g47g2000cwa.googlegroups. com>,
dr*******@gmail.com <dr*******@gmail.com> wrote:

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.
~


.... 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....utomation.html >.
Sep 1 '05 #5

P: n/a
Thierry Lam wrote:
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


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
Sep 1 '05 #6

P: n/a
"Thierry Lam" <la********@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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Sep 1 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.