C
C
code_bit2017-11-30 18:18:52
Python
code_bit, 2017-11-30 18:18:52

How to upload file to FTP using Python script in active mode?

There is a certain industrial piece of iron, I am always connected to it locally. On a piece of iron, a standard FTP server to which you need to upload a file using a Python script. If I use the standard ftp.exe client on Windows - everything loads fine with help. commands:
put "C:\путь до файла\файл"
If I knock with a script like:

USER = 'xxxx'
PASS = 'xxxx'
SERVER = '192.168.1.x'
PORT = 21
ftp = ftplib.FTP(HOST, USER, PASS)
upload_file = open("C:\file.zip", 'rb')
ftp_connetion.storbinary('STOR ' + 'file.zip', upload_file)

communication looks like this:
5a201ed20a18c584584874.png
and that's where it all stops...
Is it possible that only the active file transfer mode is available for this device, which just comes with FTP.exe and the put command? How to implement it in Python? In all the examples that could be found on the Internet, only passive mode is used along with storbinary.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Articefir, 2021-01-22
@Articefir

Hello. The storbinary method of the ftp object, not the ftp_connection object. Also, you open the file, but do not close it after all the manipulations with it. Once the ftp object has been created, authorization must be performed using the ftp.login() command. Try changing your code like this:

USER = 'xxxx'
PASS = 'xxxx'
SERVER = '192.168.1.x'
PORT = 21
ftp = ftplib.FTP(HOST, USER, PASS)
ftp.login()
# Конструкция открывает файл в заданном режиме и в любом случае закроет его
with open("C:\file.zip", 'rb') as upload_file:
    ftp.storbinary('STOR ' + 'file.zip', upload_file)
# Закрываем FTP соединение
ftp.close

If an authorization error pops up, try changing the lines:
ftp = ftplib.FTP(HOST, USER, PASS)
ftp.login()

on the:
ftp = ftplib.FTP(HOST)
ftp.login(USER, PASS)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question