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.
- 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)
- site.py ( the file containing the python code )
You start the server in a command prompt using : python site.py which will start the server. Leave the command prompt open.
You can then visit the webserver by opening a browser and going to http://localhost:8888 to see the index page and play around with it.
# # MathPoc : Proof of concept of a simple math problem, bringing it to the browser # # Alex Boschmans # # Version 0.2, February 2011 # # 0.2 Added some error checks and expanded math to not just adding but also # subtraction and multiplication and divisions. Extensively commented code. header = """""" 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
MATH Proof of Concept""" footer = "" indexhtml = """
Math Proof of Concept
Please answer the following question
How much is %d %s %d ?
The question was : %s %s %s = ?
Your answer %s is wrong. The correct answer is %d.
The question was : %s %s %s = %s
Your answer is correct !
You need to fill in an answer !