Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a faster alternative to Python's http.server (or 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...

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?

like image 300
Drew Noakes Avatar asked Oct 15 '12 23:10

Drew Noakes


People also ask

What is SimpleHTTPServer Python?

SimpleHTTPServer is a python module which allows you to instantly create a web server or serve your files in a snap. Main advantage of python's SimpleHTTPServer is you don't need to install anything since you have python interpreter installed.

Is Python a HTTP server?

Python comes with a built-in module known as SimpleHTTPServer, which in other words is a simple HTTP server that gives you standard GET and HEAD request handlers.

Is Python HTTP server good for production?

Python docs claim, that "http. server is not recommended for production. It only implements basic security checks."

What is webserver in Python?

HTTP Web Server is simply a process which runs on a machine and listens for incoming HTTP Requests by a specific IP and Port number, and then sends back a response for the request. Python has a built-in webserver provided by its standard library, can be called for simple client-server communication.


1 Answers

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 
like image 168
Drew Noakes Avatar answered Oct 02 '22 01:10

Drew Noakes