Sometimes it's convenient to run a very simple HTTP server just to serve web contents from your current directory.
In a system with Python ⇗ installed (Mac OS & many Linux Distros come with Python out of the box), we can run a simple command in the current directory to serve its web content via localhost.
For instance:
python -m SimpleHTTPServer 8000
However, SimpleHTTPServer is not asynchronous and often can't serve many large files.
We can wrap the SimpleHTTPServer with Python's threading feature in SocketServer to achieve a simple server that's capable of async serving files. Reference: Multithreaded python simple http server ⇗
#!/usr/bin/env pythonimport SocketServerimport BaseHTTPServerimport SimpleHTTPServerclass ThreadingSimpleServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):passimport sysif sys.argv[1:]:port = int(sys.argv[1])else:port = 3000server = ThreadingSimpleServer(('', port), SimpleHTTPServer.SimpleHTTPRequestHandler)try:while 1:sys.stdout.flush()server.handle_request()except KeyboardInterrupt:print "Interrupted, exiting..."
Create a file named serve.py
with content above in the current folder. Add the execute permissions
chmod +x serve.py
Then we can start serving the web content of current directory by running
./serve.py <<port>>
<<port>>
is optional and set to 3000
by default. We can then access the web content by going to http://localhost:<<port>>