Hướng Phan Ngọc
Hướng Phan Ngọc

Reputation: 29

How to Fix: Error uploading file: [WinError 10049] The requested address is not valid in its context

I am having trouble uploading files to FTP Server with error:

[WinError 10049] The requested address is not valid in its context.

My vsftpd.conf

listen=NO
listen_ipv6=YES
anonymous_enable=NO
write_enable=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
chroot_local_user=YES
allow_writeable_chroot=YES
user_sub_token=$USER
local_root=/home/$USER/ftp
secure_chroot_dir=/var/run/vsftpd/empty
pam_service_name=vsftpd
pasv_min_port=40000
pasv_max_port=50000
pasv_address=0.0.0.0
userlist_enable=NO
userlist_file=/etc/vsftpd.userlist
userlist_deny=NO
rsa_cert_file=/etc/ssl/private/vsftpd.pem
rsa_private_key_file=/etc/ssl/private/vsftpd.pem
ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
require_ssl_reuse=NO
ssl_ciphers=HIGH

My code

def connect_ftp():
    ftp = FTP_TLS(timeout=20)
    ftp.connect('my_server', 21)
    ftp.auth()
    ftp.prot_p()
    ftp.login(user= 'pnhuong', passwd='76210119')
    ftp.set_pasv(True)
    ftp.cwd("/files")
    return ftp

def upload_file(ftp_connection, upload_file_path):
    try:
        upload_file1 = open(os.path.join(upload_file_path), 'r')
        print('Uploading ' + upload_file_path + "...")
        ftp_connection.storbinary('STOR ' + upload_file_path, upload_file1)
        ftp_connection.quit()
        ftp_connection.close()
        upload_file.close()
        print('Upload finished.')
    except Exception as e:
        print("Error uploading file: " + str(e))

I tried uploading the image using FileZilla and it worked. Please help me fix this error.

The error originates from:

ftp_connection.storbinary('STOR ' + upload_file_path, upload_file1)

Upvotes: 0

Views: 729

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202594

Your FTP server is misconfigured. The pasv_address directive must be set to the address of your FTP server. The 0.0.0.0 is definitely invalid.

pasv_address=0.0.0.0

While FileZilla can heuristically workaround that, many other FTP clients and libraries will (rightfully) fail. I'm sure you will find this in the FileZilla log file:

Command: PASV
Response: 227 Entering Passive Mode (0,0,0,0,X,Y).
Status: Server sent passive reply with unroutable address. Using server address instead.


Though note that vsftpd has a bug if the server has IPv6 address. So you might also check this:
vsftpd returns 0,0,0,0 in response to PASV

Upvotes: 0

Related Questions