有什么比Python的http.server(或SimpleHTTPServer)更快的替代方法?

本文翻译自:What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

Python's http.server (or SimpleHTTPServer for Python 2) is a great way of serve the contents of the current directory from the command line: Python的http.server (或适用于Python 2的SimpleHTTPServer)是一种从命令行提供当前目录内容的好方法:

python -m http.server

However, as far as web servers go, it's very slooooow... 但是,就Web服务器而言,它是非常简单的...

It behaves as though it's single threaded, and occasionally causes timeout errors when loading JavaScript AMD modules using RequireJS. 它的行为就好像是单线程的一样,并且在使用RequireJS加载JavaScript AMD模块时偶尔会导致超时错误。 It can take five to ten seconds to load a simple page with no images. 加载没有图像的简单页面可能需要五到十秒钟。

What's a faster alternative that is just as convenient? 有什么方便的更快替代方法?


#1楼

参考:https://stackoom.com/question/S9i2/有什么比Python的http-server-或SimpleHTTPServer-更快的替代方法


#2楼

http-server for node.js is very convenient, and is a lot faster than Python's SimpleHTTPServer. 用于node.js的http服务器非常方便,并且比Python的SimpleHTTPServer快得多。 This is primarily because it uses asynchronous IO for concurrent handling of requests, instead of serialising requests. 这主要是因为它使用异步IO并发处理请求,而不是序列化请求。

Installation 安装

Install node.js if you haven't already. 如果尚未安装,请安装node.js。 Then use the node package manager ( npm ) to install the package, using the -g option to install globally. 然后使用节点软件包管理器( npm )安装软件包,并使用-g选项进行全局安装。 If you're on Windows you'll need a prompt with administrator permissions, and on Linux/OSX you'll want to sudo the command: 如果您使用Windows,则需要具有管理员权限的提示,而在Linux / OSX上,您将需要sudo命令:

npm install http-server -g

This will download any required dependencies and install http-server . 这将下载所有必需的依赖项并安装http-server

Use 采用

Now, from any directory, you can type: 现在,您可以从任何目录键入:

http-server [path] [options]

Path is optional, defaulting to ./public if it exists, otherwise ./ . 路径是可选的,如果存在则默认为./public ,否则为./

Options are [defaults]: 选项为[默认值]:

  • -p The port number to listen on [8080] -p要侦听的端口号[8080]
  • -a The host address to bind to [localhost] -a绑定到[localhost]的主机地址
  • -i Display directory index pages [True] -i显示目录索引页[是]
  • -s or --silent Silent mode won't log to the console -s--silent静默模式不会登录到控制台
  • -h or --help Displays help message and exits -h--help显示帮助消息并退出

So to serve the current directory on port 8000, type: 因此,要在端口8000上提供当前目录,请键入:

http-server -p 8000

#3楼

If you use Mercurial, you can use the built in HTTP server. 如果您使用Mercurial,则可以使用内置的HTTP服务器。 In the folder you wish to serve up: 在您要投放的文件夹中:

hg serve

From 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

#4楼

give polpetta a try ... 尝试波尔佩塔...

npm install -g polpetta npm install -g polpetta

then you can 那么你也能

polpetta ~/folder 波波塔〜/文件夹

and you are ready to go :-) 你准备好了:-)


#5楼

1.0 includes a http server & util for serving files with a few lines of code. 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 this source using go run myserver.go or to build an executable go build myserver.go 使用go run myserver.go来运行此源,或构建可执行文件go build myserver.go


#6楼

I recommend: Twisted ( http://twistedmatrix.com ) 我建议: Twistedhttp://twistedmatrix.com

an event-driven networking engine written in Python and licensed under the open source MIT license. 一个事件驱动的网络引擎,该引擎使用Python编写,并已获得MIT开源许可。

It's cross-platform and was preinstalled on OS X 10.5 to 10.12. 它是跨平台的,已预先安装在OS X 10.5至10.12上。 Amongst other things you can start up a simple web server in the current directory with: 除其他外,您可以使用以下命令在当前目录中启动一个简单的Web服务器:

twistd -no web --path=.

Details 细节

Explanation of Options (see twistd --help for more): 选项说明(有关更多信息,请参见twistd --help ):

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

"web" is a Command that runs a simple web server on top of the Twisted async engine. “ web”是在Twisted异步引擎之上运行简单Web服务器的命令。 It also accepts command line options (after the "web" command - see twistd web --help for more): 它还接受命令行选项(在“ 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.

There are also a bunch of other commands such as: 还有很多其他命令,例如:

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

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

Mac OS-X (comes preinstalled on 10.5 - 10.12, or is available in MacPorts and through Pip) Mac OS-X(预先安装在10.5-10.12上,或者可通过MacPorts和通过Pip获得)

sudo port install py-twisted

Windows 视窗

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

HTTPS HTTPS

Twisted can also utilise security certificates to encrypt the connection. Twisted还可以利用安全证书来加密连接。 Use this with your existing --path and --port (for plain HTTP) options. 与现有的--path--port (对于纯HTTP)选项一起使用。

twistd -no web -c cert.pem -k privkey.pem --https=4433
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值