Answer the question
In order to leave comments, you need to log in
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)
Answer the question
In order to leave comments, you need to log in
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
ftp = ftplib.FTP(HOST, USER, PASS)
ftp.login()
ftp = ftplib.FTP(HOST)
ftp.login(USER, PASS)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question