Python 的http.server(或 Python 2 的 SimpleHTTPServer)是从命令行提供当前目录内容的好方法:
python -m http.server
然而,就网络服务器而言,它非常缓慢......
它的行为就好像它是单线程的,并且在使用 RequireJS 加载 JavaScript AMD 模块时偶尔会导致超时错误。加载一个没有图像的简单页面可能需要五到十秒钟。
什么是同样方便的更快替代方案?
Python 的http.server(或 Python 2 的 SimpleHTTPServer)是从命令行提供当前目录内容的好方法:
python -m http.server
然而,就网络服务器而言,它非常缓慢......
它的行为就好像它是单线程的,并且在使用 RequireJS 加载 JavaScript AMD 模块时偶尔会导致超时错误。加载一个没有图像的简单页面可能需要五到十秒钟。
什么是同样方便的更快替代方案?
node.js 的http-server非常方便,而且比 Python 的 SimpleHTTPServer 快很多。这主要是因为它使用异步 IO 来并发处理请求,而不是序列化请求。
如果您还没有安装 node.js。然后使用节点包管理器 ( npm
) 安装包,使用-g
全局安装选项。如果您在 Windows 上,您将需要具有管理员权限的提示,而在 Linux/OSX 上,您将需要sudo
以下命令:
npm install http-server -g
这将下载任何所需的依赖项并安装http-server
.
现在,从任何目录中,您可以键入:
http-server [path] [options]
路径是可选的,./public
如果存在则默认为,否则为./
.
选项是 [默认值]:
-p
要侦听的端口号 [8080]-a
要绑定到 [localhost] 的主机地址-i
显示目录索引页 [True]-s
或--silent
静默模式不会登录到控制台-h
或--help
显示帮助信息并退出因此,要在端口 8000 上提供当前目录,请键入:
http-server -p 8000
我推荐:Twisted ( http://twistedmatrix.com )
一个用 Python 编写的事件驱动的网络引擎,并在开源 MIT 许可下获得许可。
它是跨平台的,预装在 OS X 10.5 到 10.12 上。您可以在当前目录中启动一个简单的 Web 服务器,其中包括:
twistd -no web --path=.
选项说明(见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 服务器。它还接受命令行选项(在“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
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/
Twisted 还可以利用安全证书来加密连接。将此与您现有的--path
和--port
(对于纯 HTTP)选项一起使用。
twistd -no web -c cert.pem -k privkey.pem --https=4433
go 1.0 包含一个http 服务器和实用程序,用于通过几行代码提供文件。
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
试试webfs,它很小,不依赖于安装 node.js 或 python 之类的平台。
如果您使用 Mercurial,则可以使用内置的 HTTP 服务器。在您希望提供的文件夹中:
hg serve
从文档:
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 中创建一个新选项卡并单击左上角附近的应用程序按钮来运行它
它有一个简单的 gui。单击选择文件夹,然后单击http://127.0.0.1:8887
链接
我发现python -m http.server
不可靠——有些响应需要几秒钟的时间。
现在我使用名为 Ran https://github.com/m3ng9i/ran的服务器
Ran:一个用 Go 编写的简单的静态 Web 服务器
如果您安装了 PHP,您可以使用内置服务器。
php -S 0:8080
试试polpetta...
npm install -g polpetta
那么你也能
polpetta ~/文件夹
你准备好了:-)
使用 Servez 作为服务器
http://localhost:8080
或选择“启动浏览器”注意:我把它放在一起是因为 Chrome 的 Web 服务器正在消失,因为Chrome 正在取消对应用程序的支持,并且因为我支持对命令行零经验的艺术学生
另一个基于节点的简单命令行服务器
https://github.com/greggman/servez-cli
部分是为了响应 http-server 的问题,特别是在 windows 上。
npm install -g servez
servez [options] [path]
没有路径,它服务于当前文件夹。
默认情况下,它index.html
用于文件夹路径(如果存在)。否则,它会为文件夹提供目录列表。它还提供 CORS 标头。您可以选择启用基本身份验证,--username=somename --password=somepass
并且可以提供 https。
我喜欢live-server。它速度快,并且具有很好的实时重新加载功能,这在开发过程中非常方便。
用法很简单:
cd ~/Sites/
live-server
默认情况下,它会创建一个 IP 为 127.0.0.1 和端口为 8080 的服务器。
如果端口 8080 不是空闲的,它使用另一个端口:
如果您需要查看本地网络中其他机器上的 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'