什么是Python的SimpleHTTPServer的更快的替代品? [英] What is a faster alternative to Python's SimpleHTTPServer?

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

问题描述

Python的SimpleHTTPServer是一种从命令行提供当前目录内容的好方法:

  python -m SimpleHTTPServer 

但是,就网络服务器而言,这是非常slooooow ...



它的行为就像是单线程,偶尔会在使用RequireJS加载JavaScript AMD模块时导致超时错误。



node.js的div class =h2_lin>解决方案

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 如果存在,否则 ./



选项是[defaults]:




  • -p 要监听的端口号[8080]

  • -a 绑定到[localhost]的主机地址

  • -i 显示目录索引页[True]

  • -s - silent 静音模式将不会记录到控制台

  • -h - help 显示帮助消息和退出



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

  http-server -p 8000 


Python's SimpleHTTPServer is a great way of serve the contents of the current directory from the command line:

python -m SimpleHTTPServer

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

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 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.

Installation

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

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

Use

Now, from any directory, you can type:

http-server [path] [options]

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

Options are [defaults]:

  • -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

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

http-server -p 8000

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

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