标签归档:ftp-server

python中的一线ftp服务器

问题:python中的一线ftp服务器

是否可以在python中使用一行命令来执行简单的ftp服务器?我希望能够以这种快速和临时的方式来执行此操作,而无需将FTP服务器安装到Linux机器上。最好是使用内置的python库的方法,因此无需额外安装。

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.


回答 0

强制性扭曲示例:

twistd -n ftp

可能有用:

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.

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.

回答 1

退房pyftpdlib从詹Rodola。它是python最好的ftp服务器之一。它用于Google的Chrome(浏览器)和Bazaar(版本控制系统)中。它是Python上最完整的RFC-959实现(又名:FTP服务器实现规范)。

在命令行中:

python -m pyftpdlib

或者’my_server.py’:

#!/usr/bin/env python

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()

如果您想要更复杂的内容,则网站上还有更多示例。

要获取命令行选项列表:

python -m pyftpdlib --help

注意,如果要覆盖或使用标准ftp端口,则需要管理员权限(例如sudo)。

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).

From the commandline:

python -m pyftpdlib

Alternatively ‘my_server.py’:

#!/usr/bin/env python

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:

python -m pyftpdlib --help

Note, if you want to override or use a standard ftp port, you’ll need admin privileges (e.g. sudo).


回答 2

您为什么不使用单行HTTP服务器呢?

python -m SimpleHTTPServer 8000

将通过HTTP在端口8000上提供当前工作目录的内容。

如果您使用Python 3,则应改写

python3 -m http.server 8000

有关2.x,请参见SimpleHTTPServer模块文档;有关3.x,请参见http.server文档。

顺便说一下,在两种情况下,port参数都是可选的。

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.


回答 3

以上所有答案均假设您的Python发行版将具有一些第三方库,以实现“一个线性python ftpd”目标,但@zio并非如此。另外,SimpleHTTPServer涉及Web浏览器来下载文件,这还不够快。

Python不能做的ftpd本身,但可以使用的netcatnc

nc从根本上说,它是任何类似UNIX的系统(甚至是嵌入式系统)的内置工具,因此非常适合“ 快速而临时的文件传输方式 ”。

步骤1,在接收方,运行:

nc -l 12345 | tar -xf -

这将侦听端口12345,等待数据。

步骤2,在发送方:

tar -cf - ALL_FILES_YOU_WANT_TO_SEND ... | nc $RECEIVER_IP 12345

你也可以放 pv中间以监视传输进度:

tar -cf - ALL_FILES_YOU_WANT_TO_SEND ...| pv | nc $RECEIVER_IP 12345

传输完成后,的双方nc将自动退出,并完成工作。

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.


回答 4

对于pyftpdlib用户。我在pyftpdlib网站上找到了这个。这将创建对您的文件系统具有写访问权的匿名ftp,因此请谨慎使用。更多功能可提供更好的安全性,所以请看看:

sudo pip3 install pyftpdlib

python3 -m pyftpdlib -w  

## updated for python3 Feb14:2020

对于尝试使用上述不推荐使用的方法的用户可能会有所帮助。

须藤python -m pyftpdlib.ftpserver

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


回答 5

安装:

pip install twisted

然后是代码:

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()

深入了解:

http://twistedmatrix.com/documents/current/core/examples/

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:

http://twistedmatrix.com/documents/current/core/examples/


回答 6

更简单的解决方案将是用户pyftpd库。该库允许您将Python FTP服务器转为一行。虽然默认情况下未安装它,但是我们可以使用简单的apt命令安装它

apt-get install python-pyftpdlib

现在从您要提供的目录中运行pythod模块

python -m pyftpdlib -p 21 

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 

回答 7

我不知道单线FTP服务器,但是如果您知道

python -m SimpleHTTPServer

它将在0.0.0.0:8000上运行HTTP服务器,以提供当前目录之外的文件。如果您正在寻找一种通过Web浏览器快速从Linux盒子中获取文件的方法,那么您将无法击败它。

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.


回答 8

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
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