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?
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.
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.
Python docs claim, that "http. server is not recommended for production. It only implements basic security checks."
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.
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.
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
.
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 exitsSo to serve the current directory on port 8000, type:
http-server -p 8000
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With