Calling ftp commands from python

T

Thierry Lam

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
 
D

draghuram

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

Robert Kern

Thierry said:
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
(e-mail address removed)

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

Steve Holden

Thierry said:
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
 
C

Cameron Laird

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.programmer/ftp_automation.html >.
 
M

Maurice LING

Thierry said:
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
 
M

Mike Meyer

Thierry Lam said:
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
 

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
474,266
Messages
2,571,318
Members
47,998
Latest member
GretaCjy4

Latest Threads

Top