FTP upload file works manually, but fails using Python ftplib

2024/9/23 9:35:59

I installed vsFTP in a Debian box. When manually upload file using ftp command, it's ok. i.e, the following session works:

john@myhost:~$ ftp xxx.xxx.xxx.xxx 5111
Connected to xxx.xxx.xxx.xxx.
220 Hello,Welcom to my FTP server.
Name (xxx.xxx.xxx.xxx:john): ftpuser
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put st.zip
local: st.zip remote: st.zip
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 File receive OK.
12773 bytes sent in 0.00 secs (277191.8 kB/s)
ftp> 221 Goodbye.

(Please noted that as above, I configured vsFTP server to use a non-default port,e.g 5111 for some reason)

Now when I write a script in python to upload file programmatically, it failed. the error says 'time out', as the following session shows:

john@myhost:~$ ipython
Python 2.5.2 (r252:60911, Jan 24 2010, 14:53:14) 
Type "copyright", "credits" or "license" for more information.IPython 0.8.4 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.In [1]: import ftplibIn [2]: ftp=ftplib.FTP()                                                    In [3]: ftp.connect('xxx.xxx.xxx.xxx','5111')                                
Out[3]: "220 Hello,Welcom to my FTP server."In [4]: ftp.login('ftpuser','ftpuser')                              
Out[4]: '230 Login successful.'In [5]: f=open('st.zip','rb')                              In [6]: ftp.storbinary('STOR %s' % 'my_ftp_file.zip', f)                            
---------------------------------------------------------------------------
error                                     Traceback (most recent call last).../usr/lib/python2.5/ftplib.pyc in ntransfercmd(self, cmd, rest)322             af, socktype, proto, canon, sa = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM)[0]323             conn = socket.socket(af, socktype, proto)
--> 324             conn.connect(sa)325             if rest is not None:326                 self.sendcmd("REST %s" % rest)/usr/lib/python2.5/socket.pyc in connect(self, *args)error: (110, 'Connection timed out')

I guess there is some wrong config in my vsFTP server, but cannot figure it out. Anyone can help ?

my vsFTP config is:

listen=YESconnect_from_port_20=YES
listen_port=5111
ftp_data_port=5110# Passive FTP mode allowed
pasv_enable=YES
pasv_min_port=5300
pasv_max_port=5400max_per_ip=2
Answer

The timeout doesn't happen until you try to send the data, so you were able to connect to the server successfully. The only difference I see is that ftplib uses passive mode by default, whereas your command-line client does not appear to. Try doing

ftp.set_pasv(False)

before initiating the transfer and see what happens.

Note that non-passive mode is essentially obsolete because it cannot be used across NAT firewalls, so you should probably configure vsFTP to allow passive mode.

https://en.xdnf.cn/q/71842.html

Related Q&A

Baktracking function which calculates change exceeds maximum recursion depth

Im trying to write a function that finds all possible combinations of coins that yield a specified amount, for example it calculates all possible way to give change for the amount 2 British pounds from…

How to interface a NumPy complex array with C function using ctypes?

I have a function in C that takes an array of complex floats and does calculations on them in-place.:/* foo.c */ void foo(cmplx_float* array, int length) {...}The complex float struct looks like this:t…

How to access predefined environment variables in conda environment.yml?

I wish to share an environment.yml file for others to reproduce the same setup as I have. The code we use depends on the environment variable $PWD. I wish to set a new env variable in the environment.y…

Python enclosing scope variables with lambda function

I wrote this simple code:def makelist():L = []for i in range(5):L.append(lambda x: i**x)return Lok, now I callmylist = makelist()because the enclosing scope variable is looked up when the nested functi…

Overloading + to support tuples

Id like to be able to write something like this in python:a = (1, 2) b = (3, 4) c = a + b # c would be (4, 6) d = 3 * b # d would be (9, 12)I realize that you can overload operators to work with custom…

Extracting particular text associated value from an image

I have an image, and from the image I want to extract key and value pair details.As an example, I want to extract the value of "MASTER-AIRWAYBILL NO:" I have written to extract the entire te…

Installing pip in Pycharm 2016.3

I upgraded to the new version of Pycharm. In the terminal, it says bash-3.2$ instead of my username. When I tried to install a library, it said that pip command is not found:bash: pip: command not foun…

How to store real-time chat messages in database?

I am using mysqldb for my database currently, and I need to integrate a messaging feature that is in real-time. The chat demo that Tornado provides does not implement a database, (whereas the blog does…

Selectively import from another Jupyter Notebook

I arranged my Jupyter notebooks into: data.ipynb, methods.ipynb and results.ipynb. How can I selectively import cells from data and methods notebooks for use in the results notebook?I know of nbimport…

supervisord event listener

Im trying to configure an event listener for supervisord but cant get it to work. I just want to listen for PROCESS_STATE changes and run some python code triggering an urllib2request.In my .conf I hav…