什么是Python的http.server(或SimpleHTTPServer)的更快的替代品?

Dre*_*kes 289 command-line command-line-tool simplehttpserver httpserver

Python的http.server(或Python的SimpleHTTPServer)是从命令行提供当前目录内容的好方法:

python -m http.server
Run Code Online (Sandbox Code Playgroud)

但是,就网络服务器而言,它非常低调......

它表现得好像是单线程的,并且在使用RequireJS加载JavaScript AMD模块时偶尔会导致超时错误.加载一个没有图像的简单页面可能需要五到十秒钟.

什么是更快的替代方案,同样方便?

Dre*_*kes 379

node.js的http-server非常方便,比Python的SimpleHTTPServer快得多.这主要是因为它使用异步IO来并发处理请求,而不是序列化请求.

安装

如果你还没有安装node.js.然后使用节点包管理器(npm)来安装包,使用-g全局安装选项.如果您使用的是Windows,则需要具有管理员权限的提示,而在Linux/OSX上,您需要使用sudo以下命令:

npm install http-server -g
Run Code Online (Sandbox Code Playgroud)

这将下载任何所需的依赖项并安装http-server.

使用

现在,从任何目录,您可以键入:

http-server [path] [options]
Run Code Online (Sandbox Code Playgroud)

路径是可选的,默认为./public是否存在,否则./.

选项是[默认值]:

  • -p 收听的端口号[8080]
  • -a 要绑定到[localhost]的主机地址
  • -i 显示目录索引页[True]
  • -s--silent静音模式不会登录到控制台
  • -h--help显示帮助信息并退出

因此,要在端口8000上提供当前目录,请键入:

http-server -p 8000
Run Code Online (Sandbox Code Playgroud)

  • 这太棒了!!谢谢你指出来.它非常适合测试流式音频/视频,这是python服务器似乎根本无法处理的东西. (11认同)
  • 我只想确认使用此解决方案将页面加载时间从20秒提高到2秒! (2认同)
  • 因此,截至 2019 年 11 月,Windows 用户的 http-server 似乎已经中断了几个月。它的许多依赖项都已经过时了。其中一个欣喜若狂,现已被废弃,因此尚不清楚何时或是否会修复它。我考虑自行修复,但也不清楚开发人员是否会接受 PR。所以,我编写了自己的[替换](https://github.com/greggman/servez-cli)。 (2认同)

Pet*_*son 103

我推荐:Twisted(http://twistedmatrix.com)

一个用Python编写并根据开源MIT许可证授权的事件驱动的网络引擎.

它是跨平台的,自10.5以来就预装在OS X上.除此之外,您还可以在当前目录中启动一个简单的Web服务器:

twistd -no web --path=.
Run Code Online (Sandbox Code Playgroud)

细节

选项说明(参见twistd --help更多信息):

-n, --nodaemon       don't daemonize, don't use default umask of 0077
-o, --no_save        do not save state on shutdown
Run Code Online (Sandbox Code Playgroud)

"web"是一个在Twisted异步引擎之上运行简单Web服务器的Command.它还接受命令行选项(在"web"命令之后 - 参见twistd web --help更多信息):

  --path=             <path> is either a specific file or a directory to be
                      set as the root of the web server. Use this if you
                      have a directory full of HTML, cgi, php3, epy, or rpy
                      files or any other files that you want to be served up
                      raw.
Run Code Online (Sandbox Code Playgroud)

还有一堆其他命令,例如:

conch            A Conch SSH service.
dns              A domain name server.
ftp              An FTP server.
inetd            An inetd(8) replacement.
mail             An email service
... etc
Run Code Online (Sandbox Code Playgroud)

安装

Ubuntu的

sudo apt-get install python-twisted-web (or python-twisted for the full engine)
Run Code Online (Sandbox Code Playgroud)

Mac OS-X(自10.5以来预装,或在MacPorts中可用)

sudo port install py-twisted
Run Code Online (Sandbox Code Playgroud)

视窗

installer available for download at http://twistedmatrix.com/
Run Code Online (Sandbox Code Playgroud)

HTTPS

Twisted还可以利用安全证书来加密连接.将其与现有--path--port(对于普通HTTP)选项一起使用.

twistd -no web -c cert.pem -k privkey.pem --https=4433
Run Code Online (Sandbox Code Playgroud)

  • 使用节点没有为我正确传输视频/音频,使用扭曲的作品虽然很棒! (7认同)
  • 除非你已经设置了node.js,否则我发现这是最方便的.感谢分享! (5认同)
  • 在Ubuntu上,你必须先"sudo apt-get install python-twisted-web".(感谢您的回答,非常方便!) (3认同)
  • 扭曲的一线服务器的一个特殊优势,它支持可恢复的下载(字节范围支持),并且当您下载大文件时这是必须具备的功能. (2认同)
  • 您可以使用`twistd --help`和`twistd web --help`配置端口并获取其他选项.我花了一段时间来弄清楚这一点. (2认同)

pd4*_*d40 27

1.0包含一个http服务器util,用于提供几行代码的文件.

package main

import (
    "fmt"; "log"; "net/http"
)

func main() {
    fmt.Println("Serving files in the current directory on port 8080")
    http.Handle("/", http.FileServer(http.Dir(".")))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

使用go run myserver.go或运行此源来构建可执行文件go build myserver.go


Hud*_*don 21

尝试使用webfs,它很小,并且不依赖于安装node.js或python这样的平台.

  • 我在我的Mac上做了一个`brew install webfs`,结果是`/usr/local/Cellar/webfs/1.21:5个文件,96K,内置15秒`.之后我可以说`webfsd -F -p 3003 -r resources/public/-f index.html`实现与`twistd -no web -p 3003 --path = resources/public /`相同.它有点罗嗦,所以要记住这一点并不明显,但要知道它是替代twistd或SimpleHTTPServer的好方法. (3认同)
  • 是的,除非你的发行版有它.Debian和Ubuntu有它:`apt-get install webfs` (2认同)

Dre*_*kes 14

如果使用Mercurial,则可以使用内置的HTTP服务器.在您要提供的文件夹中:

hg serve
Run Code Online (Sandbox Code Playgroud)

来自文档:

export the repository via HTTP

    Start a local HTTP repository browser and pull server.

    By default, the server logs accesses to stdout and errors to
    stderr. Use the "-A" and "-E" options to log to files.

options:

 -A --accesslog       name of access log file to write to
 -d --daemon          run server in background
    --daemon-pipefds  used internally by daemon mode
 -E --errorlog        name of error log file to write to
 -p --port            port to listen on (default: 8000)
 -a --address         address to listen on (default: all interfaces)
    --prefix          prefix path to serve from (default: server root)
 -n --name            name to show in web pages (default: working dir)
    --webdir-conf     name of the webdir config file (serve more than one repo)
    --pid-file        name of file to write process ID to
    --stdio           for remote clients
 -t --templates       web templates to use
    --style           template style to use
 -6 --ipv6            use IPv6 in addition to IPv4
    --certificate     SSL certificate file

use "hg -v help serve" to show global options
Run Code Online (Sandbox Code Playgroud)


gma*_*man 11

这是另一个.这是Chrome扩展程序

安装完成后,您可以通过在Chrome中创建新标签并点击左上角附近的应用按钮来运行它

它有一个简单的gui.单击选择文件夹,然后单击http://127.0.0.1:8887链接

在此输入图像描述

https://www.youtube.com/watch?v=AK6swHiPtew


gma*_*man 8

还要考虑devd一个用go编写的小型web服务器.这里提供了许多平台的二进制文件.

devd -ol path/to/files/to/serve
Run Code Online (Sandbox Code Playgroud)

它小巧,快速,并提供一些有趣的可选功能,如文件更改时的实时重新加载.


Col*_*nic 7

我觉得python -m http.server不可靠 - 有些回复需要几秒钟.

现在我使用名为Ran https://github.com/m3ng9i/ran的服务器

Ran:用Go编写的简单静态Web服务器


小智 5

尝试波尔佩塔...

npm install -g polpetta

那么你也能

波波塔〜/文件夹

你准备好了:-)

  • 您能否详细说明*为什么*我们应该尝试一下 [polpetta](https://www.npmjs.com/package/polpetta)? (5认同)

use*_*607 5

如果您安装了 PHP,则可以使用内置服务器。

php -S 0:8080
Run Code Online (Sandbox Code Playgroud)