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?
I need to implement a very simple web-server-like app in Python which would perform
Share
You can use socket programming for this purpose. The following snippet creates a tcp socket and listens on port 9000 for http requests:
Start the server,
$ python server.py.Open
http://localhost:9000/in your web-browser (which acts as client). Then in the browser window, you can see the text “Hello World” (http response).EDIT**
The previous code was only tested on chrome, and as you guys suggested about other browsers, the code was modified as:
shutdown()needs to be called socket.shutdown vs socket.closeThen the code was tested on chrome, firefox (http://localhost:9000/) and simple curl in terminal (curl http://localhost:9000).