LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2008, 08:39 AM   #1
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
python - download from ftp


Hi, I've been trying to write a short program that logs in to my NAS drive (ftp account), lists the contents of the /data/notes/ directory and downloads a file of my choice.

It works when I explicitly specify the file in the script,
eg.

Code:
file_to_download = s.retrbinary('RETR myfile.txt', open('myfile.txt', 'wb').write)
However, if I want to see the contents of the /data/notes directory first and then choose the right file to download it gives an error:


Code:
#! /usr/bin/env python

import ftplib


def nas():
    # connect to the ftp server
    s = ftplib.FTP('192.168.1.102', 'user', 'passwd')
    print "List of files"
    s.cwd("/DATA/temp/")
    files = s.dir()
    print files
    
    ask = raw_input("Choose the file to download: ")
    file_to_download = s.retrbinary('RETR ask', open('ask', 'wb').write)
    
    s.quit()

nas()

I tried to get rid of single quotes or replaced them with double quotes - but it doesn't work.

Thanks for any help
 
Old 08-23-2008, 09:56 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
what kind of errors? did you manage to print the files?
 
Old 08-23-2008, 10:00 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
You need to understand the difference between variables and values.

Code:
>>> "a string" # double quotes are the same as single quotes
'a string'
>>> 'a string'
'a string'
>>> var='value of var'
>>> var
'value of var'
>>> 'var' # this is just another string
'var'
therefore

Code:
file_to_download = s.retrbinary('RETR ask', open(ask, 'wb').write)
no quotes
 
Old 08-23-2008, 10:34 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
Thanks guys,

That's the error with single quotes

Code:
[xtd8865@localhost python_files]$ ./ser.py
List of files
drw-rw-rw-   1 user     group           0 Jun  9 00:15 .
drw-rw-rw-   1 user     group           0 Jun  9 00:15 ..
-rw-rw-rw-   1 user     group         230 Jun  9 00:17 secre
-r--r--r--   1 user     group          41 Jun  9 00:26 6a55w01d5
-r--r--r--   1 user     group        6714 Jun  9 00:26 _wget_
-r--r--r--   1 user     group          41 Jun  9 00:26 config_samba
-r--r--r--   1 user     group         756 Jun  9 00:26 defcom_key
-r--r--r--   1 user     group       13090 Jun  9 00:26 find_tutorial
-r--r--r--   1 user     group       17771 Jun  9 00:26 goth
-r--r--r--   1 user     group           0 Jun  9 00:26 koka
-r--r--r--   1 user     group        6493 Jun  9 00:26 nfs_howto
-r--r--r--   1 user     group        2134 Jun  9 00:26 nis
-r--r--r--   1 user     group       18622 Jun  9 00:26 sed2
-r--r--r--   1 user     group       19635 Jun  9 00:26 sed_notes
None
Choose the file to download: nis
Traceback (most recent call last):
  File "./ser.py", line 27, in <module>
    nas()
  File "./ser.py", line 15, in nas
    plik = s.retrbinary('RETR ask', open('ask', 'wb').write)
  File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
    conn = self.transfercmd(cmd, rest)
  File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
    return self.getresp()
  File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.

that's what happens if I get rid of single quotes


Code:
[xtd8865@localhost python_files]$ ./ser.py
List of files
drw-rw-rw-   1 user     group           0 Jun  9 00:15 .
drw-rw-rw-   1 user     group           0 Jun  9 00:15 ..
-rw-rw-rw-   1 user     group         230 Jun  9 00:17 secre
-r--r--r--   1 user     group          41 Jun  9 00:26 6a55w01d5
-r--r--r--   1 user     group        6714 Jun  9 00:26 _wget_
-r--r--r--   1 user     group          41 Jun  9 00:26 config_samba
-r--r--r--   1 user     group         756 Jun  9 00:26 defcom_key
-r--r--r--   1 user     group       13090 Jun  9 00:26 find_tutorial
-r--r--r--   1 user     group       17771 Jun  9 00:26 goth
-r--r--r--   1 user     group           0 Jun  9 00:26 koka
-r--r--r--   1 user     group        6493 Jun  9 00:26 nfs_howto
-r--r--r--   1 user     group        2134 Jun  9 00:26 nis
-r--r--r--   1 user     group       18622 Jun  9 00:26 sed2
-r--r--r--   1 user     group       19635 Jun  9 00:26 sed_notes
None
Choose the file to download: nis
Traceback (most recent call last):
  File "./ser.py", line 27, in <module>
    nas()
  File "./ser.py", line 15, in nas
    plik = s.retrbinary('RETR ask', open(ask, 'wb').write)
  File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
    conn = self.transfercmd(cmd, rest)
  File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
    return self.getresp()
  File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.
 
Old 08-23-2008, 10:57 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Code:
s.retrbinary('RETR ask'
you are asking to retrieve the file "ask" instead, which does not exist. Still remember how to concatenate ?
 
Old 08-24-2008, 08:22 AM   #6
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
hmm, I guess I still remember how to do it... or do I?


Code:
#! /usr/bin/env python
import ftplib
def nas():
    # connect to the ftp server
    s = ftplib.FTP('192.168.1.102', 'usr', 'pswd')
    print "List of files"
    s.cwd("/DATA/temp/")
    files = s.dir()
    print files
    
    ask = raw_input("Choose the file to download: ")
    file_to_download = s.retrbinary('RETR' + ask, open(ask, 'wb').write)
    s.quit()
nas()
Code:
...
Choose the file to download: nis
Traceback (most recent call last):
  File "./ser.py", line 27, in <module>
    nas()
  File "./ser.py", line 15, in nas
    file_to_download = s.retrbinary('RETR' + ask, open(ask, 'wb').write)
  File "/usr/lib/python2.5/ftplib.py", line 390, in retrbinary
    conn = self.transfercmd(cmd, rest)
  File "/usr/lib/python2.5/ftplib.py", line 356, in transfercmd
    return self.ntransfercmd(cmd, rest)[0]
  File "/usr/lib/python2.5/ftplib.py", line 327, in ntransfercmd
    resp = self.sendcmd(cmd)
  File "/usr/lib/python2.5/ftplib.py", line 241, in sendcmd
    return self.getresp()
  File "/usr/lib/python2.5/ftplib.py", line 216, in getresp
    raise error_perm, resp
ftplib.error_perm: 550 Requested action not taken.

Any hints?
thank you

Last edited by sycamorex; 08-24-2008 at 08:36 AM.
 
Old 08-24-2008, 08:43 AM   #7
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,836

Original Poster
Blog Entries: 1

Rep: Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251Reputation: 1251
There was no space in:
('RETR' +.....

It was supposed to be:
('RETR ' +....

thanks guys
 
Old 01-05-2011, 02:22 PM   #8
andrapgm03
Member
 
Registered: Nov 2010
Location: Indonesia, jakarta
Distribution: Ubuntu Desktop 10.10
Posts: 32

Rep: Reputation: 0
use this code...hope usefull..

Code:
#!/usr/bin/env python

from ftplib import FTP
import getpass

def main():
        user=raw_input('user: ')
        passwd=getpass.getpass('passwordnya: ')
        ftp=FTP('122.200.6.124')
        ftp.login(user,passwd)

#       ret=ftp.rename('/home/andrewraharjo/welcom','/home/andrewraharjo/welcome,msg')
#       print ret

        ret=ftp.mkd('home/iniftp')
        print ret

        ret=ftp.rmd('/home/andrewraharjo/hapusini')
        print ret

        ftp.quit()

if __name__=='__main__':
        main()
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
download from ftp sauravjung Linux - Newbie 1 04-21-2008 09:43 AM
python script download torrents shanenin Programming 2 09-29-2005 01:03 PM
What FTP and download manager do you use? ICO Slackware 18 08-25-2004 08:50 AM
FTP DOWNlOAD tangaz Linux - Software 1 12-02-2003 08:10 PM
Can not download from ftp, help me !!! futurist General 1 04-15-2002 05:47 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration