Created
May 23, 2020 17:40
-
-
Save vperezb/1aa8bd146f786312307031cdb03a0ace to your computer and use it in GitHub Desktop.
Download an image from internet and upload it to ftp server
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
| import io | |
| import requests | |
| from PIL import Image | |
| import ftplib | |
| credentials = { | |
| 'host': 'blabla.com', | |
| 'user': 'myuser', | |
| 'password': 'mypass' | |
| } | |
| def get_ftp_session(credentials = None): | |
| if credentials is None: | |
| credentials = __credentials | |
| return ftplib.FTP(credentials['host'], credentials['user'], credentials['password']) | |
| def upload_to_ftp(source_filename, output_filename, credentials = None): | |
| if credentials is None: | |
| credentials = __credentials | |
| ftp_session = get_ftp_session(credentials=credentials) | |
| file= open(source_filename, 'rb') | |
| ftp_session.storbinary('STOR ' + output_filename, file) | |
| file.close() | |
| return ftp_session.quit() | |
| url_image = 'https://myurl.to.download' | |
| response = requests.get(url_image) | |
| pilimage = Image.open(io.BytesIO(response.content)) | |
| output_file_name = "test.jpg" | |
| pilimage.save(output_file_name) | |
| upload_to_ftp(output_file_name, '/ftp_path/another_folder' + '/' + output_file_name, credentials=credentials) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment