Home > Net >  Upload file with ftplib not working and without any error message?
Upload file with ftplib not working and without any error message?

Time:12-21

i try to upload an image to my ftp-server using ftplib with the following code -

import ftplib
import os
import sys

ADDR = "68.66.248.00"
USERNAME = "MyUser"
PW = "MyPw"

path = os.path.abspath(os.path.dirname(sys.argv[0])) 
fn = os.path.join(path, "test.png")

session = ftplib.FTP()
session.connect(ADDR)
session.login(USERNAME, PW)
session.cwd("tmp/")
session.dir()

file = open(fn,'rb')  
print("A")          
session.storbinary('testXYZ.png', file) 
print("B")          
file.close()                             
session.quit()

When i run this code i get this output:

$ python testFTP.py
drwxr-xr-x    7 rapidtec   rapidtec         4096 Sep 20 00:16 .
drwx--x--x   37 rapidtec   rapidtec         4096 Dec 20 09:49 ..
drwx------    2 rapidtec   rapidtec         4096 Oct 12  2020 analog
drwx------    2 rapidtec   rapidtec         4096 Oct 12  2020 awstats
drwx------    2 rapidtec   rapidtec         4096 Oct 12  2020 webalizer

A

As you can see the program stopps working at the line with "session.storbinary". When i run this in vscode even the terminal is not anymore interruptable using Ctrl-C...

Why is the upload to the ftp-server not working and without any error?

It seems that the session-creation, and the session.cwd works fine but the file-transfer is for whatever reason not working

Why is this ftp-upload not working?

CodePudding user response:

I'm not sure if it is your only problem (as I would not expect your code to hang this way), but in any case, your FTP.storbinary call is incorrect. You are missing the FTP transfer command (typically STOR). It should be:

session.storbinary('STOR testXYZ.png', file) 

Additionally (while not a problem as such), as the right pattern you should use the with statement:

with open(fn,'rb') as file:
    session.storbinary('testXYZ.png', file)
  • Related