It's still changing now ... When I was touching a legacy system, I had a situation where I had to upload FTP to an FTP server, so this is a memo when I decided to do it with python.
# -*- coding: utf-8 -*-
import ftplib
import logging
def ftp_upload(hostname, username, password, port, upload_src_path, upload_dst_path, timeout):
logger.info({
'action': 'ftp_upload',
'status': 'run'
})
#FTP connection/upload
with ftplib.FTP() as ftp:
try:
ftp.connect(host=hostname, port=port, timeout=timeout)
#Passive mode setting
ftp.set_pasv("true")
#FTP server login
ftp.login(username, password)
with open(upload_src_path, 'rb') as fp:
ftp.storbinary(upload_dst_path, fp)
except ftplib.all_errors as e:
logger.error({
'action': 'ftp_upload',
'message': 'FTP error = %s' % e
})
logger.info({
'action': 'ftp_upload',
'status': 'success'
})
#log settings
logger = logging.getLogger(__name__)
formatter = '%(asctime)s:%(name)s:%(levelname)s:%(message)s'
logging.basicConfig(
filename='./ftp_logger.log',
level=logging.DEBUG,
format=formatter
)
logger.setLevel(logging.INFO)
#Host name of the server to connect to
hostname = "FTP server IP"
#File path to upload
upload_src_path = "./test.jpg "
#Upload destination file path
upload_dst_path = "STOR /test.jpg "
#Server username
username = "Each username"
#Server login password (
password = "Each password"
#FTP server port
port = 21
timeout = 50
logger.info("===START FTP===")
ftp_upload(hostname, username, password, port, upload_src_path, upload_dst_path, timeout)
logger.info("===FINISH FTP===")
As you can see in the reference
https://intellectual-curiosity.tokyo/2019/12/01/python%E3%81%A7ftp%E3%82%A2%E3%83%83%E3%83%97%E3%83%AD%E3%83%BC%E3%83%89%E3%82%92%E8%A1%8C%E3%81%86%E6%96%B9%E6%B3%95/
I created it with reference to. It's very easy to understand. Thank you very much.
that's all.
・ Https://algorithm.joho.info/programming/python/ftp-file-upload-server/ ・ Https://intellectual-curiosity.tokyo/2019/12/01/python%E3%81%A7ftp%E3%82%A2%E3%83%83%E3%83%97%E3%83%AD%E3 % 83% BC% E3% 83% 89% E3% 82% 92% E8% A1% 8C% E3% 81% 86% E6% 96% B9% E6% B3% 95 / ・ Https://qiita.com/init/items/91e5841ed53d55a7895e
Recommended Posts