python中的一行ftp服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4994638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 18:31:40  来源:igfitidea点击:

One line ftp server in python

pythonftpftp-server

提问by zio

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.

是否可以在 python 中使用一行命令来做一个简单的 ftp 服务器?我希望能够以快速和临时的方式将文件传输到 linux 机器,而无需安装 ftp 服务器。最好是使用内置 python 库的方法,这样就不需要安装任何额外的东西了。

采纳答案by Ali Afshar

Obligatory Twistedexample:

强制性扭曲示例:

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.

回答by Alex

I dont know about a one-line FTP server, but if you do

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

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.

它将在 0.0.0.0:8000 上运行一个 HTTP 服务器,为当前目录之外的文件提供服务。如果您正在寻找一种使用 Web 浏览器快速从 linux 机器上获取文件的方法,那么您无法击败它。

回答by Andrea Spadaccini

Why don't you instead use a one-line HTTPserver?

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

python -m SimpleHTTPServer 8000

will serve the contents of the current working directory over HTTP on port 8000.

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

If you use Python 3, you should instead write

如果你使用 Python 3,你应该写

python3 -m http.server 8000

See the SimpleHTTPServermodule docs for 2.x and the http.serverdocs for 3.x.

请参阅2.x的SimpleHTTPServer模块文档和3.x的http.server文档。

By the way, in both cases the port parameter is optional.

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

回答by Joe Drumgoole

Good list of tools at

很好的工具清单在

http://www.willdonnelly.net/blog/file-transfer/

http://www.willdonnelly.net/blog/file-transfer/

I've used woof myself on a number of occasions. Very nice.

我在很多场合都使用过woof。非常好。

回答by Brian Bruggeman

Check out pyftpdlibfrom 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).

退房pyftpdlib从詹Rodola。它是 Python 中最好的 ftp 服务器之一。它被用在谷歌的chromium(他们的浏览器)和bazaar(一个版本控制系统)中。它是RFC-959(又名:FTP 服务器实现规范)在 Python 上最完整的实现。

From the commandline:

从命令行:

python -m pyftpdlib

Alternatively 'my_server.py':

或者'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).

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

回答by cdplayer

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:

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

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

须藤 python -m pyftpdlib.ftpserver

回答by Meow

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 发行版将具有一些第三方库以实现“one liner python ftpd”目标,但这不是@zio 所要求的情况。此外,SimpleHTTPServer 涉及用于下载文件的网络浏览器,速度不够快。

Python can't do ftpd by itself, but you can use netcat, nc:

Python 不能自己做 ftpd,但你可以使用netcat, nc

ncis 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".

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

Step 1, on the receiver side, run:

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

nc -l 12345 | tar -xf -

this will listen on port 12345, waiting for data.

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

Step 2, on the sender side:

第 2 步,在发送方:

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

You can also put pvin the middle to monitor the progress of transferring:

也可以放在pv中间来监控传输的进度:

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

After the transferring is finished, both sides of ncwill quit automatically, and job done.

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

回答by Jonathan

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/

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

回答by MVnD3X

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

更简单的解决方案是用户 pyftpd 库。这个库允许你在一行中旋转 Python FTP 服务器。虽然默认情况下它没有安装,但我们可以使用简单的 apt 命令安装它

apt-get install python-pyftpdlib

now from the directory you want to serve just run the pythod module

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

python -m pyftpdlib -p 21 

回答by Shashwot Risal

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