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

问题描述 投票:280回答:11

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

python -m http.server

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

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

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

command-line httpserver command-line-tool simplehttpserver
11个回答
379
投票

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

Installation

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

npm install http-server -g

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

Use

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

http-server [path] [options]

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

选项是[默认值]:

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

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

http-server -p 8000

3
投票

使用Servez作为服务器

  1. 下载Servez
  2. 安装它,运行它
  3. 选择要投放的文件夹
  4. 选择“开始”
  5. 转到http://localhost:8080或选择“启动浏览器”

注意:我把它放在一起是因为自从Chrome is removing support for apps以来,Web Server for Chrome正在消失,因为我支持那些对命令行没有经验的艺术学生


2
投票

我喜欢live-server。它速度快,具有良好的实时重载功能,在开发过程中非常方便。

用法很简单:

cd ~/Sites/
live-server

默认情况下,它会创建一个IP 127.0.0.1和端口8080的服务器。

http://127.0.0.1:8080/

如果端口8080不是空闲的,则它使用另一个端口:

http://127.0.0.1:52749/

http://127.0.0.1:52858/

如果您需要在本地网络中的其他计算机上查看Web服务器,则可以检查您的IP和使用情况:

live-server --host=192.168.1.121

这是一个自动获取默认接口IP地址的脚本。它仅适用于macOS。

如果你把它放在.bash_profile中,live-server命令将自动启动具有正确IP的服务器。

# **
# Get IP address of default interface
# *
function getIPofDefaultInterface()
{
    local  __resultvar=$1

    # Get default route interface
    if=$(route -n get 0.0.0.0 2>/dev/null | awk '/interface: / {print $2}')
    if [ -n "$if" ]; then
            # Get IP of the default route interface
            local __IP=$( ipconfig getifaddr $if )
            eval $__resultvar="'$__IP'"
    else
        # Echo "No default route found"
        eval $__resultvar="'0.0.0.0'"
    fi
}

alias getIP='getIPofDefaultInterface IP; echo $IP'

# **
# live-server
# https://www.npmjs.com/package/live-server
# *
alias live-server='getIPofDefaultInterface IP && live-server --host=$IP'

103
投票

我推荐:扭曲(http://twistedmatrix.com

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

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

twistd -no web --path=.

Details

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

-n, --nodaemon       don't daemonize, don't use default umask of 0077
-o, --no_save        do not save state on shutdown

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

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

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

Installation

Ubuntu的

sudo apt-get install python-twisted-web (or python-twisted for the full engine)

Mac OS-X(预装在10.5 - 10.12,或在MacPorts和Pip中提供)

sudo port install py-twisted

视窗

installer available for download at http://twistedmatrix.com/

HTTPS

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

twistd -no web -c cert.pem -k privkey.pem --https=4433

26
投票

1.0包括http serverutil for serving files以及几行代码。

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

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


20
投票

试试webfs,它很小,不依赖于安装node.js或python这样的平台。


15
投票

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

hg serve

来自the docs

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

11
投票

Here's another. It's a Chrome Extension

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

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

enter image description here

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


8
投票

还要考虑devd一个用go编写的小型web服务器。许多平台的二进制文件可用here

devd -ol path/to/files/to/serve

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


7
投票

我发现python -m http.server不可靠 - 一些回复需要几秒钟。

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

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


5
投票

试试polpetta ......

npm install -g polpetta

然后你可以

肉丸〜/文件夹

你准备好了:-)

© www.soinside.com 2019 - 2024. All rights reserved.