My Webhost

Meta

Proof of concept for a simple webserver running python code

Here is a small code example using CherryPy to run a very simple webserver that

generates a simple math question

compares the answer to the solution.

It’s meant as a proof of concept, so there is no security built in. It’s running on localhost on port 8888 (modifiable in the main part of the code).
It allows you to play around and test out your ideas.

Do not use this code on an outside network !

It’s simply an example showing how easy it is to set up a web server and how you can create pages for it using python and CherryPy. It’s been cobbled together in an evening from previous programming so there’s some cruft left in. I’ve also extensively commented the code.

Requirements:

python 2.7 ( 2.5 will work as well is my guess )

cherrypy 3.2.2 ( use easy_install or pip to download and install the latest version)

Math Proof of Concept

Please answer the following question

How much is %d %s %d ?

Answer:
"""
def generatequestion():
# This generates the question that we will pose using the random function
# Generate a random question using 2 random numbers between 1 and 10
number1 = random.randint(1,10)
number2 = random.randint(1,10)
# Now we choose an operatioin
ops = ["+", "-", "x", "/"]
operation = random.choice(ops)
# Let's check the division
if operation == "/":
# Prevent divisions with remainders using the modulo operator
# Using module on the two numbers evaluates to 0 when no remainder is present
# While the modulo remainder is not equal to 0, generate two new numbers
while number1 % number2 <> 0:
number1 = random.randint(1,10)
number2 = random.randint(1,10)
# Assemble the html, inputting the numbers in the foreseen places in the html
# In a more extensive project, you would keep this html in a template file and
# call it with a dictionary of items that need to be filled in the template
question = indexhtml % (number1, operation, number2, number1, number2, operation)
# Add common html like header and footer - these are defined just once and reused
# for each page
html = header + question + footer
# Return the completed html to the calling function (in this case index)
return html
# This is the class that the cherrypy server uses and where you create the views that the
# webuser sees. After each definition there is a .exposed=True that indicates if the
# webuser can see this page or not.
class MathPoc:
def index(self):
# This is the main index page that is shown to the user when he first visits the site.
# We create the page by calling the function generatequestion (which is outside the class
# MathPoc but accessible and we show it to the user by 'return'ing the page
page = generatequestion()
return page
# The webuser will now see the page and will have a chance to enter an answer.
# In the html form I've specified that the submitted result will go to the url "response"
# I've added all the values I want to receive either as hidden values (eg the
# original numbers, the operation) or as part of the form (eg the answer)
index.exposed = True
def response(self, answer, number1, number2, operation):
# First check if we received an answer or if the user submitted without an answer
if answer:
# Calculate our own answer ourselves and generate a response to the user
# We receive strings, so convert them to integers using int()
number1 = int(number1)
number2 = int(number2)
answer = int(answer)
# Answer is dependent on operation
if operation == "+":
solution = number1 + number2
elif operation == "-":
solution = number1 - number2
elif operation == "x":
solution = number1 * number2
else:
solution = number1 / number2
# See if the answer is correct and display according the result
# Using templates, you could put all this in one template and
# call the template with options so it knows what to show
if solution <> answer:
html = """

3 thoughts on “Proof of concept for a simple webserver running python code”

Thank you so much for posting this. I am such a newbie that it is going to take me some time to go through this and understand it. But I really appreciate that you would take the time to generate it in response to my question.

Ok, so here are some (really,really) newbie questions:

1. How do you upgrade Python from ver 2.5 to 2.7. I have been googling it, but it seems like it is one of those computer questions that is so obvious to computer veterans that it is not worth posting. Do you uninstall 2.5 and download 2.7? Can you then run the programs that you have already written?

2. Your post says that you need site.py. Does this mean I need to copy and paste the code or can I download the file?

Hey I am glad you liked it – feel free to ask me questions on the code and whatnot, no problem, I’ll respond if I can.

As for your current questions :
1. upgrade of python : I don’t think it’s really necessary to upgrade to 2.7.x as I’ve run CherryPy on 2.5 before, but yes you can uninstall 2.5 and download and install the latest version of Python – you can even keep the two python installations separately. If you haven’t programmed much or installed additional python libraries, the simplest would be to uninstall 2.5 and reinstall 2.7.

As long as you keep your python programs in a separate directory you can still use those programs.

I would not recommend downloading the 3.x of Python – not that it’s bad or so, but it uses a different syntax, is more strict and my code would probably break on it.

If you are starting from the very beginning, to use easy_install to install CherryPy you will need to install easy_install as well : you can find installers for easy_install on PyPi. Once installed, just use ‘easy_install cherrypy’

2. Downloading the file is probably the easiest – as Python depends on indentations for it’s code, copy-pasting the above code might lead to problems converting from html. Just download the file and remove the .txt part of the file keeping the .py extension

I’ve updated the post with code which is more extensively commented to better learn from and have improved the “generatequestion” function so it also does multiplication, division and subtracting instead of just adding.

I might just translate it the html pages to Dutch and let my six-year old train on it !

We use cookies to ensure that we give you the best experience on our website, to provide personal recommendations and to analyse our traffic using Google Analytics. If you continue to use this site we will assume that you are happy with it. Ok