什么是 Python 的 http.server(或 SimpleHTTPServer)的更快替代方案? [英] What is a faster alternative to Python's http.server (or SimpleHTTPServer)?

查看:20
本文介绍了什么是 Python 的 http.server(或 SimpleHTTPServer)的更快替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 的 http.server(或 Python 2 的 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 -m http.server

然而,就网络服务器而言,它非常慢......

However, as far as web servers go, it's very slooooow...

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

It behaves as though it's single threaded, and occasionally causes timeout errors when loading JavaScript AMD modules using RequireJS. 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?

推荐答案

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

http-server for node.js is very convenient, and is a lot faster than Python's SimpleHTTPServer. This is primarily because it uses asynchronous IO for concurrent handling of requests, instead of serialising requests.

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

Install node.js if you haven't already. Then use the node package manager (npm) to install the package, using the -g option to install globally. If you're on Windows you'll need a prompt with administrator permissions, and on Linux/OSX you'll want to sudo the command:

npm install http-server -g

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

This will download any required dependencies and install http-server.

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

Now, from any directory, you can type:

http-server [path] [options]

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

Path is optional, defaulting to ./public if it exists, otherwise ./.

选项是[默认]:

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

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

So to serve the current directory on port 8000, type:

http-server -p 8000

这篇关于什么是 Python 的 http.server(或 SimpleHTTPServer)的更快替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆