Once again, this is going to be a simple tutorial on how to create a simple web server. But this time I will be using python and Bottle web framework.
First things first. Assuming you have python installed on your PC, lets install pip which is a tool for installing python packages. apt-get install pip is the command that you would easily use within a linux system. You could also go to their website and get it downloaded right away.
Now that you have pip installed, lets install the web framework:
pip install bottle
Now its time for the code. Inside your .py file, lets first import the required methods:
from bottle import route, run, template
Next, using the route method we set a URL that the server would listen on:
@route('/welcome/<name>')
where name is a variable that the program would handle. Basically our goal is to print a welcome message along with the name passed after 'welcome/' in the URL. To achieve that, we shall use:
def index(name):
return template('<h1>Welcome {{text}}</h1>', text=name)
Next, we setup the address and port on which the server would be listening on:
run(host='127.0.0.1', port=8080)
Complete code
from bottle import route, run, template
@route('/welcome/<name>')
def index(name):
return template('<h1>Welcome {{text}}!</h1>', text=name)
run(host='127.0.0.1', port=8080)