Is it possible to have a one line command in python to do a simple ftp server? I'd like to be able to do this as quick and temporary way to transfer files to a linux box without having to install a ftp server. Preferably a way using built in python libraries so there's nothing extra to install.
-
8Unless you use an external python library like pftftpdlib, I doubt you can do this with one line of code. Python has a built in ftp client, but not a server. – GWW Feb 14 '11 at 16:40
-
4transfer files to linux??? why don't you just use scp? – Leonmax Dec 02 '14 at 17:52
-
Why not use **sshfs**? For fastest transfer rates (in a LAN) use option `-o Ciphers=arcfour`, i.e. `sshfs $REMOTEHOST:$DIRECTORY $MOUNTPOINT -o Ciphers=arcfour` and copy, move, rename, manage your files in your filesystem. sshfs is also available for Mac OSX. – erik Jul 27 '15 at 10:05
9 Answers
Obligatory Twisted example:
twistd -n ftp
And probably useful:
twistd ftp --help
Usage: twistd [options] ftp [options].
WARNING: This FTP server is probably INSECURE do not use it.
Options:
-p, --port= set the port number [default: 2121]
-r, --root= define the root of the ftp-site. [default:
/usr/local/ftp]
--userAnonymous= Name of the anonymous user. [default: anonymous]
--password-file= username:password-style credentials database
--version
--help Display this help and exit.

- 38,334
- 103
- 306
- 551

- 40,967
- 12
- 95
- 109
-
8Very nice! But the OP asked to use the standard python library because he didn't want to install other libraries. – Andrea Spadaccini Feb 14 '11 at 17:03
-
26Right, I do deserve down-voting, but really, people should know Twisted. And of course Twisted is in the base install of many Linuxes. – Ali Afshar Feb 14 '11 at 17:42
-
2I didn't down-vote because it's a nice answer, ever if a bit O, and people from the future will find it useful when they search for similar problems. – Andrea Spadaccini Feb 14 '11 at 17:52
-
I tried that, accessing from the web timeouts, accessing with telnet asks for a USER and a PASSWORD. – user3779430 May 07 '15 at 13:03
-
@AliAfshar Does this work on Windows too? I tried to install twisted using the Anaconda distribution but [haven't had any luck](http://stackoverflow.com/q/30903001/2766558). – Michael A Jun 18 '15 at 16:48
-
1@MichaelA Should certainly work on windows. p.s. It's weird visiting my own future. – Ali Afshar Apr 04 '16 at 05:09
-
https://twistedmatrix.com/trac/ for those, like me, who had not heard of Twisted. I will edit the question to add the link (only; no further change) – Mawg says reinstate Monica Apr 05 '16 at 07:44
-
2On most platforms twisted install is as easy as `pip install -U twisted` which is not directly mentioned on the linked site. – Steve Barnes Apr 07 '16 at 05:56
-
1OP asked for a one-liner, so can you please this one line using twist? I'm serious - I tried to use twist (python3), but I was not able to find the right arguments such that a) I am able to upload somthing and b) not my entire home gets exposed via ftp. – Thorsten Feb 28 '18 at 12:21
-
FYI, I tried twistd ftp, but were unable to upload file (permission error, cannot understand why). `pyftpdlib` worked as expected from the first shot. – Stéphane Apr 02 '21 at 13:16
-
This does not actually work for uploads. Even with passing `--root` and using the `memory` auth string, `strace` shows that it tries to walk the home directory. No joy. – Jan Kundrát May 06 '22 at 05:37
Check out pyftpdlib from Giampaolo Rodola. It is one of the very best ftp servers out there for python. It's used in google's chromium (their browser) and bazaar (a version control system). It is the most complete implementation on Python for RFC-959 (aka: FTP server implementation spec).
To install:
pip3 install pyftpdlib
From the commandline:
python3 -m pyftpdlib
Alternatively 'my_server.py':
#!/usr/bin/env python3
from pyftpdlib import servers
from pyftpdlib.handlers import FTPHandler
address = ("0.0.0.0", 21) # listen on every IP on my machine on port 21
server = servers.FTPServer(address, FTPHandler)
server.serve_forever()
There's more examples on the website if you want something more complicated.
To get a list of command line options:
python3 -m pyftpdlib --help
Note, if you want to override or use a standard ftp port, you'll need admin privileges (e.g. sudo).

- 5,008
- 2
- 36
- 55
-
1As of 1.3.0: `>>> from pyftpdlib import ftpserver`: `pyftpdlib\ftpserver.py:54: DeprecationWarning: pyftpdlib.ftpserver module is deprecated` – Daniel Reis Dec 30 '13 at 11:44
-
2Updated Wed Aug 27, 2014 for current version. Shouldn't have deprecation warnings, now. – Brian Bruggeman Aug 27 '14 at 16:28
-
-
-
@BrianBruggeman No by secure I mean not exposing ftp over internet, not doing anything unintended and not stealing my data, I mean is the source code secure enough to be used in production environments? – Amir Hossein Baghernezad Dec 28 '17 at 04:22
-
Unfortunately pyftpdlib is not a builtin in Python 3 anymore, leaving us without a replacement – adamczi Jul 15 '20 at 07:32
-
1pyftpdlib was never a builtin. You would need to use pip3 to install: `pip3 install pyftpdlib`. – Brian Bruggeman Feb 22 '21 at 20:44
Why don't you instead use a one-line HTTP server?
python -m SimpleHTTPServer 8000
will serve the contents of the current working directory over HTTP on port 8000.
If you use Python 3, you should instead write
python3 -m http.server 8000
See the SimpleHTTPServer module docs for 2.x and the http.server docs for 3.x.
By the way, in both cases the port parameter is optional.

- 12,378
- 5
- 40
- 54
-
44
-
3
-
@RomanPlášil it will, just run server on another end xD simply as it is – Reishin Mar 31 '18 at 20:19
-
8
-
The answers above were all assuming your Python distribution would have some third-party libraries in order to achieve the "one liner python ftpd" goal, but that is not the case of what @zio was asking. Also, SimpleHTTPServer involves web broswer for downloading files, it's not quick enough.
Python can't do ftpd by itself, but you can use netcat, nc
:
nc
is basically a built-in tool from any UNIX-like systems (even embedded systems), so it's perfect for "quick and temporary way to transfer files".
Step 1, on the receiver side, run:
nc -l 12345 | tar -xf -
this will listen on port 12345, waiting for data.
Step 2, on the sender side:
tar -cf - ALL_FILES_YOU_WANT_TO_SEND ... | nc $RECEIVER_IP 12345
You can also put pv
in the middle to monitor the progress of transferring:
tar -cf - ALL_FILES_YOU_WANT_TO_SEND ...| pv | nc $RECEIVER_IP 12345
After the transferring is finished, both sides of nc
will quit automatically, and job done.
-
6Speaking of assuming... You assume that the clients are going to be unix machines with `tar` and `nc` available. What if it's windows? – jlh Jan 02 '19 at 20:26
-
wow this is convenient. Would there be any GUI wrapper build around this? I wouldn't be able to pitch command line way to my folks, a simple UI would make it happen – Nikhil VJ Jun 24 '21 at 09:28
-
1@jlh You can use it via Cygwin on Windows, it seems. Cygwin's not a tiny install, but if you need to do much Linux-like stuff on the command-line in Windows, it should be convenient and easy. – Brōtsyorfuzthrāx Apr 12 '22 at 22:28
For pyftpdlib users. I found this on the pyftpdlib website. This creates anonymous ftp with write access to your filesystem so please use with due care. More features are available under the hood for better security so just go look:
sudo pip3 install pyftpdlib
python3 -m pyftpdlib -w
## updated for python3 Feb14:2020
Might be helpful for those that tried using the deprecated method above.
sudo python -m pyftpdlib.ftpserver

- 495
- 5
- 15
-
3
-
1don't forget that there's the v3 variant: `pip3 install pyftpdlib` and `python3 -m pyftpdlib -w`. worked like a charm for me. – Ron Klein Jun 02 '19 at 05:14
-
thanks! This works beautifully for quick transfer between computers at home. – Nikhil VJ Jun 24 '21 at 09:44
-
apt-get install python3-pip
pip3 install pyftpdlib
python3 -m pyftpdlib -p 21 -w --user=username --password=password
-w = write permission
-p = desired port
--user = give your username
--password = give your password

- 121
- 1
- 4
-
1@karel They cover the same library, but they do not look like duplicates to me. – Mark Rotteveel May 03 '20 at 07:35
Install:
pip install twisted
Then the code:
from twisted.protocols.ftp import FTPFactory, FTPRealm
from twisted.cred.portal import Portal
from twisted.cred.checkers import AllowAnonymousAccess, FilePasswordDB
from twisted.internet import reactor
reactor.listenTCP(21, FTPFactory(Portal(FTPRealm('./'), [AllowAnonymousAccess()])))
reactor.run()
Get deeper:

- 6,741
- 7
- 52
- 69
The simpler solution will be to user pyftpd library. This library allows you to spin Python FTP server in one line. It doesn’t come installed by default though, but we can install it using simple apt command
apt-get install python-pyftpdlib
now from the directory you want to serve just run the pythod module
python -m pyftpdlib -p 21

- 65
- 9
-
Looks good, but what kind of authentication does happen? I get "530 Authentication failed." – guettli Sep 18 '18 at 13:33
I dont know about a one-line FTP server, but if you do
python -m SimpleHTTPServer
It'll run an HTTP server on 0.0.0.0:8000, serving files out of the current directory. If you're looking for a way to quickly get files off a linux box with a web browser, you cant beat it.

- 4,316
- 2
- 24
- 28