-
-
Save tochimclaren/1f33d82182a0287319e1a68b2d21fc66 to your computer and use it in GitHub Desktop.
ftp upload and download scripts [python, ftplib]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Download a file from ftp server.""" | |
import argparse | |
import logging | |
import os | |
from ftplib import FTP_TLS | |
logging.getLogger().setLevel(logging.DEBUG) | |
def main(args): | |
ftps = FTP_TLS() | |
ftps.connect(args.host, args.port) | |
logging.debug('Connected') | |
ftps.login(args.user, args.password) | |
logging.debug('Logged in') | |
ftps.prot_p() | |
download_file(ftps, args.file, args.directory) | |
ftps.quit() | |
logging.debug('Connection closed') | |
def download_file(ftps, source, directory): | |
"""Download file from ftp server. | |
:param ftps: FTP server connection | |
:type ftps: ftplib.FTP_TLS | |
:param source: Path to the remote file to be downloaded. | |
:type source: str | |
:param path: Path to the local directory where file will be written to. | |
:type path: str | |
""" | |
logging.debug('Downloading file: %s', source) | |
basename = os.path.basename(source) | |
destination = os.path.join(directory, basename) | |
with open(destination, 'wb') as fp: | |
def callback(data): | |
"""Write received data to destination.""" | |
fp.write(data) | |
ftps.retrbinary('RETR {}'.format(source), callback) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | |
parser.add_argument( | |
'--host', | |
default='127.0.0.1', | |
help='FTP server host (%(default)s by default)', | |
) | |
parser.add_argument( | |
'--port', | |
type=int, | |
default=21, | |
help='FTP server port (%(default)s by default)', | |
) | |
parser.add_argument('-u', '--user', help='User') | |
parser.add_argument('-p', '--password', help='User password') | |
parser.add_argument( | |
'file', | |
help='Path to remote file that will be downloaded', | |
) | |
parser.add_argument( | |
'directory', | |
help='Path to local directory where file will be written', | |
) | |
args = parser.parse_args() | |
main(args) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Upload either a file or all files in a directory to ftp server.""" | |
import argparse | |
import logging | |
import os | |
from ftplib import FTP_TLS | |
logging.getLogger().setLevel(logging.DEBUG) | |
def main(args): | |
ftps = FTP_TLS() | |
ftps.connect(args.host, args.port) | |
logging.debug('Connected') | |
ftps.login(args.user, args.password) | |
logging.debug('Logged in') | |
ftps.prot_p() | |
# Note: this assumes that all files in the given directory should be uploaded | |
if os.path.isdir(args.path): | |
upload_directory(ftps, args.path) | |
elif os.path.isfile(args.path): | |
upload_file(ftps, args.path) | |
ftps.quit() | |
logging.debug('Connection closed') | |
def upload_directory(ftps, path): | |
"""Upload all files in a given directory. | |
:param path: Path to the directory that contains files to upload. | |
:type path: str | |
""" | |
basenames = os.listdir(args.path) | |
for i, basename in enumerate(basenames): | |
fname = os.path.join(args.dir, basename) | |
with open(fname) as fp: | |
logging.debug( | |
'(%d/%d) Uploading file: %s', | |
i + 1, | |
len(basenames), | |
fname, | |
) | |
ftps.storbinary('STOR {}'.format(basename), fp) | |
def upload_file(ftps, path): | |
"""Upload file to ftp server. | |
:param path: Path to the file to be uploaded. | |
:type path: str | |
""" | |
logging.debug('Uploading file: %s', args.path) | |
basename = os.path.basename(args.path) | |
with open(args.path, 'rb') as fp: | |
ftps.storbinary('STOR {}'.format(basename), fp) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) | |
parser.add_argument( | |
'--host', | |
default='127.0.0.1', | |
help='FTP server host (%(default)s by default)', | |
) | |
parser.add_argument( | |
'--port', | |
type=int, | |
default=21, | |
help='FTP server port (%(default)s by default)', | |
) | |
parser.add_argument('-u', '--user', help='User') | |
parser.add_argument('-p', '--password', help='User password') | |
parser.add_argument( | |
'path', | |
help='Path (either a single file or a directory with files to be uploaded)', | |
) | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment