What is a faster alternative to Python's http.server (or SimpleHTTPServer)?
Python的http.server(或Python 2的SimpleHTTPServer)是从命令行提供当前目录内容的一种好方法:
1 | python -m http.server |
但是,就Web服务器而言,它是非常简单的...
它的行为就像是单线程的一样,并且在使用RequireJS加载JavaScript AMD模块时偶尔会导致超时错误。 加载没有图像的简单页面可能需要五到十秒钟。
有什么更便捷的替代方案又方便吗?
用于node.js的http服务器非常方便,并且比Python的SimpleHTTPServer快得多。这主要是因为它使用异步IO并发处理请求,而不是序列化请求。
安装
如果尚未安装,请安装node.js。然后,使用节点软件包管理器(
1 | npm install http-server -g |
这将下载所有必需的依赖项并安装
采用
现在,您可以从任何目录键入:
1 | http-server [path] [options] |
路径是可选的,如果存在,则默认为
选项为[默认值]:
-
-p 要侦听的端口号[8080] -
-a 绑定到[localhost]的主机地址 -
-i 显示目录索引页[是] -
-s 或--silent 静默模式不会登录到控制台 -
-h 或--help 显示帮助消息并退出
因此,要在端口8000上提供当前目录,请键入:
1 | http-server -p 8000 |
我建议:Twisted(http://twistedmatrix.com)
an event-driven networking engine written in Python and licensed under the open source MIT license.
它是跨平台的,已预先安装在OS X 10.5至10.12上。除其他外,您可以使用以下命令在当前目录中启动一个简单的Web服务器:
1 | twistd -no web --path=. |
细节
选项说明(有关更多信息,请参见
1 2 | -n, --nodaemon don't daemonize, don't use default umask of 0077 -o, --no_save do not save state on shutdown |
" web"是一个在Twisted异步引擎之上运行简单Web服务器的命令。它还接受命令行选项(在" web"命令之后-有关更多信息,请参见
1 2 3 4 5 | --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. |
还有许多其他命令,例如:
1 2 3 4 5 6 | conch A Conch SSH service. dns A domain name server. ftp An FTP server. inetd An inetd(8) replacement. mail An email service ... etc |
安装
的Ubuntu
1 | sudo apt-get install python-twisted-web (or python-twisted for the full engine) |
Mac OS-X(预先安装在10.5-10.12上,或者可通过MacPorts和通过Pip获得)
1 | sudo port install py-twisted |
视窗
1 | installer available for download at http://twistedmatrix.com/ |
HTTPS
Twisted还可以利用安全证书来加密连接。与现有的
1 | twistd -no web -c cert.pem -k privkey.pem --https=4433 |
go 1.0包含一个http服务器和util,用于使用几行代码来提供文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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) } } |
使用
尝试使用webfs,它很小,并且不依赖于安装诸如node.js或python之类的平台。
如果您使用Mercurial,则可以使用内置的HTTP服务器。在您要投放的文件夹中:
1 | hg serve |
从文档:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | 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 |
这是另一个这是Chrome扩展程序
安装完成后,您可以通过在Chrome中创建新标签页并点击左上角附近的应用程序按钮来运行它
它有一个简单的GUI。单击选择文件夹,然后单击
还可以考虑使用devd用go编写的小型Web服务器。这里提供了许多平台的二进制文件。
1 | devd -ol path/to/files/to/serve |
它小巧,快速,并提供了一些有趣的可选功能,例如在文件更改时实时重新加载。
我发现
现在我使用一个名为Ran的服务器https://github.com/m3ng9i/ran
Ran: a simple static web server written in Go
试一试polpetta ...
npm install -g polpetta
那么你就可以
polpetta ~/folder
你准备好了:-)
使用Servez作为服务器
注意:之所以将这些汇总在一起是因为Chrome不再提供对Web服务器的支持,因为Chrome删除了对应用程序的支持,并且我支持对命令行经验为零的美术专业学生
我喜欢现场服务器。它速度快,并具有良好的实时重新加载功能,在开发过程中非常方便。
用法很简单:
1 2 | 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地址并使用:
1 | live-server --host=192.168.1.121 |
这是一个脚本,可自动获取默认接口的IP地址。它仅适用于macOS。
如果将其放在
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # ** # 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' |
如果安装了PHP,则可以使用内置服务器。
1 | php -S 0:8080 |
另一个基于节点的简单命令行服务器
https://github.com/greggman/servez-cli
部分是为了响应http服务器出现问题而写的,尤其是在Windows上。
安装
然后安装node.js
1 | npm install -g servez |
用法
1 | servez [options] [path] |
没有路径,它将提供当前文件夹。
默认情况下,它为文件夹路径提供