0

I am writing a python script to get logged in using ftp and download a file.But whenever I run this script,it says I have provided wrong user name or passwd.I am inputting right password still i am unable to run this script.My code is:

    import os,getpass
    from urllib.request import urlopen
    filename='68544.jpg'
    password=getpass.getpass('??')

At this line below the script is failed to run and whenever i run this address in browser it runs fine.

    remoteaddr='ftp://Kamal:%s@localhost/%s;type=i'%(password,filename)
    remotefile=urlopen(remoteaddr)
    localfile=open(filename,'wb')
    localfile.write(remotefile.read())
    localfile.close()
    remotefile.close()
Kme
  • 103
  • 1
  • 11
  • Have you tried [ftplib](https://docs.python.org/2/library/ftplib.html) it can be much easier, try this [post](http://stackoverflow.com/q/11768214/1982962) – Kobi K Nov 09 '14 at 09:23
  • yeah but i wanted to know how urllib works.so i was trying this but its not working. – Kme Nov 09 '14 at 09:27

2 Answers2

0
    def ftp_connect(path):
         link = FTP(host = 'example.com', timeout = 5) #Keep low timeout
         link.login(passwd = 'ftppass', user = 'ftpuser')
         debug("%s - Connected to FTP" % strftime("%d-%m-%Y %H.%M"))
         link.cwd(path)
         return link

    downloaded = open('/local/path/to/file.tgz', 'wb')
    def debug(txt):
         print txt

    link = ftp_connect(path)
    file_size = link.size(filename)
    max_attempts = 5 #I dont want death loops.
    while file_size != downloaded.tell():
      try:
            debug("%s while > try, run retrbinary\n" % strftime("%d-%m-%Y %H.%M"))
            if downloaded.tell() != 0:
                   link.retrbinary('RETR ' + filename, downloaded.write, downloaded.tell())
            else:
                   link.retrbinary('RETR ' + filename, downloaded.write)
      except Exception as myerror:
            if max_attempts != 0:
                 debug("%s while > except, something going wrong: %s\n \tfile lenght is: %i > %i\n"(strftime("%d-%m-%Y %H.%M"), myerror, file_size, downloaded.tell()))
                 link = ftp_connect(path)
                 max_attempts -= 1
            else:
                 break
      debug("Done with file, attempt to download m5dsum")

[...]

Kme
  • 103
  • 1
  • 11
0

Use Paramiko library

import paramiko
paramiko.util.log_to_file('/tmp/paramiko.log')

# Open a transport

host = "example.com"
port = 22
transport = paramiko.Transport((host, port))

# Auth

password = "foo"
username = "bar"
transport.connect(username = username, password = password)

# Go!

sftp = paramiko.SFTPClient.from_transport(transport)

# Download

### It is relative path from the folder path on which this sftp user has default rights.
filepath = 'folder/file1.txt'           
localpath = '/opt/backup/file.txt'
sftp.get(filepath, localpath)

# Close

sftp.close()
transport.close()
  • Sir I know there are many alternatives available,I know this,but I dont want to use them,I am confused why urllib is not working.Is there any problem in my code?? – Kme Nov 09 '14 at 16:05