Find the Greatest Common Denominator
This is a cgi (Common Gateway Interface)
version for running on a server
when initiated from a browser
|
The html to prompt for numbers
<!DOCTYPE html>
<html>
<body>
<form action="/cgi-bin/gcd-python1.cgi" method="post">
<label><b>Please enter two numbers</b></label>
<input type="number" name="num1">
<input type="number" name="num2">
<input type="submit" value="Submit" />
</form>
</body>
</html>
Note: you can also use javascript to prompt for the name rather than a form
See <a href="js-example1.html" TARGET="_blank">Javascript example1</a>
The Python cgi code on the server looks like this:
#!/usr/bin/python
# Import modules for CGI handling
import cgi, cgitb
def gcd(a, b):
while b:
a, b = b, a % b
return a
# Create instance of FieldStorage
form = cgi.FieldStorage()
# Get data from fields
a = form.getvalue('num1')
b = form.getvalue('num2')
# Convert string to number
from decimal import *
x = Decimal(a)
y = Decimal(b)
print "Content-type:text/html\r\n\r\n"
print "<b>GCD of %d and %d is: </b>" % (x, y)
print "<b> ", gcd(x,y), "</b>"
|