January 7, 2019
Minimal Python web server
Introduction
There are various ways to implement a minimal Python web service or server. It can be easily implemented using the web.py python web framework. First, you need to install the library. Open a terminal and execute the following command…
1 2 3 |
sudo easy_install web.py # You can also use pip sudo pip install web.py |
Provide the password when requested. If the installation goes smoothly, create a Python script and insert the following code…
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#!/usr/bin/env python import web # Define a list of end points along with corresponding request handlers # Only one is available here, you can add as many requests as you need urls = ( '/api/v1/my_request', 'handle_get_my_request' ) # Define the web app web_app = web.application(urls, globals()) # The get handler returns a tiny HTML page class handle_get_my_request: def GET(self): return "<html><center><b>Hello</b></center></html>" # Start the web server if __name__ == "__main__": web_app.run() |
Launch the web server by typing…
1 |
python test.py |
To check if the server is working as expected, open a browser window and point to
1 |
http://127.0.0.1:8080/api/v1/my_request |
If everything is fine, you should get Hello printed in the middle of the screen.
Thanks for reading. Please leave a comment if you have any feedback.