本文介绍了有什么比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

但是,就Web服务器而言,这是非常糟糕的事情...

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-服务器 node.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显示目录索引页[是]
  • -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)更快的替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 13:37