Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I implement a simple web server using Python without using any libraries?

I need to implement a very simple web-server-like app in Python which would perform basic HTTP requests and responses and display very basic output on the web page. I am not too concerned about actually coding it in Python, but I am not sure where to start? How to set this up? One file? Multiple files? I guess I have no idea how to approach the fact that this is a "server" - so I am unfamiliar with how to approach dealing with HTTP requests/sockets/processing requests, etc. Any advice? Resources?

like image 209
antonpug Avatar asked Apr 10 '12 15:04

antonpug


1 Answers

You should look at the SimpleHttpServer (py3: http.server) module.

Depending on what you're trying to do, you can either just use it, or check out the module's source (py2, py3) for ideas.

If you want to get more low-level, SimpleHttpServer extends BaseHttpServer (source) to make it just work.

If you want to get even more low-level, take a look at SocketServer (source: py2, py3).

People will often run python like python -m SimpleHttpServer (or python3 -m http.server) if they just want to share a directory: it's a fully functional and... simple server.

like image 195
quodlibetor Avatar answered Oct 26 '22 11:10

quodlibetor