Python 为当前目录提供服务的简单文件服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15328623/
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 19:49:57  来源:igfitidea点击:

Simple file server to serve current directory

pythonrubyperlnode.jsshell

提问by Reactormonk

I'm looking for a dead simple bin that I can launch up in the shell and have it serve the current directory (preferably not ..), with maybe a -pfor specifying port. As it should be a development server, it should by default allow connections from localhost only, maybe with an option to specify otherwise. The simpler, the better.

我正在寻找一个简单的垃圾箱,我可以在 shell 中启动它并让它为当前目录提供服务(最好不是 ..),也许 a-p用于指定端口。因为它应该是一个开发服务器,所以默认情况下它应该只允许来自 localhost 的连接,也许可以选择另外指定。越简单越好。

Not sure which tags to use here.

不确定在这里使用哪些标签。

采纳答案by David Pope

python3 -m http.server

or if you don't want to use the default port 8000

或者如果您不想使用默认端口 8000

python3 -m http.server 3333

or if you want to allow connections from localhost only

或者如果您只想允许来自本地主机的连接

python3 -m http.server --bind 127.0.0.1

See the docs.

请参阅文档



The equivalent Python 2 commands are

等效的 Python 2 命令是

python -m SimpleHTTPServer

python -m SimpleHTTPServer 3333

There is no --bindoption.

没有--bind选择。

See the Python 2 docs.

请参阅Python 2 文档



In case your server files do not change, once you have edited and saved them, type refreshin your python console. This will update the files, provided by the server with the most recent ones.

如果你的服务器文件没有改变,一旦你编辑并保存了它们,refresh在你的 python 控制台中输入。这将使用最新的文件更新服务器提供的文件。

回答by Joel Berger

There is the Perl app App::HTTPThisor I have often used a tiny Mojoliciousserver to do this. See my blog postfrom a while back.

有 Perl 应用程序App::HTTPThis或者我经常使用小型Mojolicious服务器来执行此操作。前段时间看我的博文

Make a file called say server.pl. Put this in it.

制作一个名为 say 的文件server.pl。把这个放进去。

#!/usr/bin/env perl

use Mojolicious::Lite;

use Cwd;
app->static->paths->[0] = getcwd;

any '/' => sub {
  shift->render_static('index.html');
};

app->start;

Install Mojolicious: curl get.mojolicio.us | shand then run morbo server.pl.

安装 Mojolicious:curl get.mojolicio.us | sh然后运行morbo server.pl.

Should work, and you can tweak the script if you need to.

应该可以工作,如果需要,您可以调整脚本。

回答by Blender

For Node, there's http-server:

对于节点,有http-server

$ npm install -g http-server
$ http-server Downloads -a localhost -p 8080
Starting up http-server, serving Downloads on port: 8080
Hit CTRL-C to stop the server

Python has:

Python有:

  • Python 3: python -m http.server --bind 127.0.0.1 8080
  • Python 2: python -m SimpleHTTPServer 8080
  • 蟒蛇3python -m http.server --bind 127.0.0.1 8080
  • 蟒蛇2python -m SimpleHTTPServer 8080

Note that Python 2 has no --bindoption, so it will allow all connections (not just from localhost).

请注意,Python 2 没有--bind选项,因此它将允许所有连接(不仅仅是来自localhost)。

回答by Cristian Ciupitu

Using Twisted Web:

使用扭曲的网络

twistd --pidfile= -n web --path .  --port 8080

--pidfile=disables the PID file. Without it a twistd.pidfile will be created in the current directory. You can also use --pidfile ''.

--pidfile=禁用 PID 文件。如果没有它,twistd.pid将在当前目录中创建一个文件。您也可以使用--pidfile ''.