TankorSmash

needed syntax highlight

Apr 16th, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.95 KB | None | 0 0
  1. # ftptest.py - An example application using Python's ftplib module.
  2. # Author: Matt Croydon <[email protected]>, referencing many sources, including:
  3. #   Pydoc for ftplib: http://web.pydoc.org/2.2/ftplib.html
  4. #   ftplib module docs: http://www.python.org/doc/current/lib/module-ftplib.html
  5. #   Python Tutorial: http://www.python.org/doc/current/tut/tut.html
  6. # License: GNU GPL.  The software is free, don't sue me.
  7. # This was written under Python 2.2, though it should work with Python 2.x and greater.
  8.  
  9. # Import the FTP object from ftplib
  10. from ftplib import FTP
  11.  
  12. # This will handle the data being downloaded
  13. # It will be explained shortly
  14. def handleDownload(block):
  15.     file.write(block)
  16.     print ".",
  17.    
  18. # Create an instance of the FTP object
  19. # Optionally, you could specify username and password:
  20. # FTP('hostname', 'username', 'password')
  21. ftp = FTP('ftp.cdrom.com')
  22.  
  23. print 'Welcome to Matt's ftplib example'
  24. # Log in to the server
  25. print 'Logging in.'
  26. # You can specify username and password here if you like:
  27. # ftp.login('username', 'password')
  28. # Otherwise, it defaults to Anonymous
  29. print ftp.login()
  30.  
  31. # This is the directory that we want to go to
  32. directory = 'pub/simtelnet/trumpet/winsock'
  33. # Let's change to that directory.  You kids might call these 'folders'
  34. print 'Changing to ' + directory
  35. ftp.cwd(directory)
  36.  
  37. # Print the contents of the directory
  38. ftp.retrlines('LIST')
  39.  
  40. # Here's a file for us to play with.  Remember Trumpet Winsock?
  41. filename = 'winap21f.zip'
  42.  
  43. # Open the file for writing in binary mode
  44. print 'Opening local file ' + filename
  45. file = open(filename, 'wb')
  46.  
  47. # Download the file a chunk at a time
  48. # Each chunk is sent to handleDownload
  49. # We append the chunk to the file and then print a '.' for progress
  50. # RETR is an FTP command
  51. print 'Getting ' + filename
  52. ftp.retrbinary('RETR ' + filename, handleDownload)
  53.  
  54. # Clean up time
  55. print 'Closing file ' + filename
  56. file.close()
  57.  
  58. print 'Closing FTP connection'
  59. print ftp.close()
Advertisement
Add Comment
Please, Sign In to add comment