[Python] I will upload the FTP to the FTP server.

Introduction

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.

Contents

Overview


# -*- 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===")

Details

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.

reference

・ 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

[Python] I will upload the FTP to the FTP server.
Upload text file to rental server by ftp
I want to display the progress in Python!
I tried to graph the packages installed in Python
I tried to touch the CSV file with Python
I tried to solve the soma cube with python
I wanted to use the Python library from MATLAB
I want to inherit to the back with python dataclass
[Python] I tried to graph the top 10 eyeshadow rankings
I want to write in Python! (3) Utilize the mock
I tried to solve the problem with Python Vol.1
I felt that I ported the Python code to C ++ 98.
I want to use the R dataset in python
I tried to summarize the string operations of Python
I downloaded the python source
I tried to find the entropy of the image with python
I want to initialize if the value is empty (python)
I tried to simulate how the infection spreads with Python
I wanted to solve the Panasonic Programming Contest 2020 with Python
maya Python I want to fix the baked animation again.
What I did to welcome the Python2 EOL with confidence
[Python] I want to use the -h option with argparse
Build a Python environment and transfer data to the server
[Python] I asked LINE BOT to answer the weather forecast.
[Python] I tried to visualize the follow relationship of Twitter
I didn't know how to use the [python] for statement
I tried to implement the mail sending function in Python
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I tried to divide the file into folders with Python
I tried to output the access log to the server using Node.js
Leave the troublesome processing to Python
I tried to touch Python (installation)
How to get the Python version
[Python] How to import the library
I calculated the stochastic integral (I to integral)
How to use FTP with Python
I wrote the queue in Python
I want to debug with Python
I tried to move the ball
I wrote the stack in Python
File upload to Azure Storage (Python)
I tried to estimate the interval.
[Python] Change the alphabet to numbers
I tried to solve the ant book beginner's edition with python
I want to know the weather with LINE bot feat.Heroku + Python
I created a Python library to call the LINE WORKS API
I want to output the beginning of the next month with Python
I want to run the Python GUI when starting Raspberry Pi
I wrote the code to write the code of Brainf * ck in python
Hit the New Relic API in Python to get the server status
I tried to display the video playback time (OpenCV: Python version)
I wanted to solve the ABC164 A ~ D problem with Python
Terminal association from the server side to Amazon SNS (python + boto3)
Regularly upload files to Google Drive using the Google Drive API in Python
I tried to improve the efficiency of daily work with Python
I want to pin Spyder to the taskbar
What skills do I need to program with the FBX SDK Python?
[Python] I tried to visualize the night on the Galactic Railroad with WordCloud!
I was able to print the thermal printer "PAPERANG" from Python (Windows10, Python3.6)
[Python] I tried to summarize the set type (set) in an easy-to-understand manner.