Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
177 views11 pages

Python CGI Scripts for Beginners

The document describes three Python CGI programs: 1. A form to collect a user's first and last name and display a welcome message. 2. A course registration form that inserts data into a database and displays it. 3. A form to look up a student's roll number in a database and display their exam results.

Uploaded by

Kavibharath R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
177 views11 pages

Python CGI Scripts for Beginners

The document describes three Python CGI programs: 1. A form to collect a user's first and last name and display a welcome message. 2. A course registration form that inserts data into a database and displays it. 3. A form to look up a student's roll number in a database and display their exam results.

Uploaded by

Kavibharath R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Ex.

No: 7 Common Gateway Interface

1. Write a CGI based python program to get first name and last name from
user through html form and display welcome message.

Aim:
To write a CGI based python program to get the name of user through html form and
display welcome message.

Program:
Python Code:
import cgi
import cgitb
form = cgi.FieldStorage()
first_name = form.getvalue('first_name')
last_name = form.getvalue('last_name')
print('Content-type:text/html\r\n\r\n')
print(f'''
<html>
<head>
<title>Hello - CGI Program</title>
</head>
<body>
<h2>Hello {first_name} {last_name}</h2>
<body>
</html>
''')

R.Kavibharath
1901102
HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello</title>
</head>
<body>
<form action="/cgi-bin/hello.py" method="get">
First Name : <input type="text" name="first_name" /> <br />
Last Name : <input type="text" name="last_name" /> <br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

Output:
HTML Form:

Welcome message:

Result:
Thus, the CGI based python program to get the user name through html form and
display welcome message was executed successfully.

R.Kavibharath
1901102
2. Create a course registration form using Python CGI. The registration
details should be updated in the Database as well as it should be displayed
in the browser.

Aim:
To create a python-based CGI script for course registration form using
database tables and to display details in the browser.

Program:
Python Code:
import cgi
import cgitb
import pymongo
client=pymongo.MongoClient("mongodb://localhost:27017/")
db=client['Course']['Course']
form=cgi.FieldStorage()
roll=form.getvalue('roll')
name=form.getvalue('name')
dob=form.getvalue('dob')
gender=form.getvalue('gender')
phone=form.getvalue('phone')
email=form.getvalue('email')
address=form.getvalue('address')
course=form.getvalue('course')
query={"Roll no":roll,
"Name":name,
"D.o.B":dob,
"Gender":gender,
"Mobile Number":phone,
"Email ID":email,

R.Kavibharath
1901102
"Course":course}
stud=db.insert_one(query)
print('Content-Type:text/html\n')
print(f'''
<html>
<head>
<title>Details</title>
</head>
<body>
<center><h3>Inserted Values</h3></center>
<p><b>Roll no</b>: {roll}</p>
<p><b>Name</b>: {name}</p>
<p><b>Date of Birth</b>: {dob}</p>
<p><b>Gender</b>: {gender}</p>
<p><b>Mobile Number</b>: {phone}</p>
<p><b>Email ID</b>: {email}</p>
<p><b>Address</b>: {address}</p>
<p><b>Course</b>: {course}</p>
</body>
</html>
''')

R.Kavibharath
1901102
HTML Code:
<!DOCTYPE html>
<html>
<head>
<title>Course Registration Form</title>
</head>
<body>
<form action="/cgi-bin/course.py" method="post">
<label for="name">Roll no: </label>
<input type="text" id="roll" name="roll" value=""><br><br>
<label for="name">Name: </label>
<input type="text" id="name" name="name" value=""><br><br>
<label for="dob">Date of Birth: </label>
<input type="date" id="dob" name="dob"><br><br>
<label for="name">Gender: </label>
<input type="text" id="gender" name="gender" value=""><br><br>
<label for="Mobile">Mobile Number: </label>
<input type="text" id="phone" name="phone"><br><br>
<label for="email">Email ID: </label>
<input type="email" id="email" name="email"><br><br>
<label for="address">Address: </label><br>
<textarea name="address" rows="5" cols="30"></textarea><br><br>
<label for="course">Course: </label>
<input type="text" id="course" name="course" value=""><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

R.Kavibharath
1901102
Output:
HTML Form:

Details in the browser:

R.Kavibharath
1901102
Updated database table:

Result:
Thus, the python-based CGI script for course registration form was created and
executed successfully.

R.Kavibharath
1901102
3. Create a python-based CGI script for result publishing. When the roll no
is given as input through CGI form, the mark should be checked in the DB
and the result should be displayed to the user.

Aim:
To create a python-based CGI script for result publishing.

Program:
Python Code:
import cgi
import cgitb
import pymongo
client=pymongo.MongoClient("mongodb://localhost:27017/")
db=client['Stud']['Student']
form=cgi.FieldStorage()
roll=form.getvalue('roll')
query={"Roll no":int(roll)}
stud=db.find_one(query)
Language=stud['Language']
English=stud['English']
Maths=stud['Maths']
Physics=stud['Physics']
Chemistry=stud['Chemistry']
Computer=stud['Computer']
Name=stud['Name']
print("Content-Type:text/html\n")
print(f'''
<html>
<head>
<title>Student Result</title>

R.Kavibharath
1901102
</head>
<body>
<h2><center>Result of <strong>{Name}<strong></center></h2><br><hr><br>
<center>
<table>
<tr>
<th>S.No</th>
<th>Subject</th>
<th>Marks</th>
<th>Result</th>
</tr>
<tr>
<td>&emsp;&emsp;1</td>
<td>&emsp;&emsp;Language</td>
<td>&emsp;&emsp;{Language}</td>
<td>&emsp;&emsp;{'Pass' if Language > 50 else 'Fail'}</td>
</tr>
<tr>
<td>&emsp;&emsp;2</td>
<td>&emsp;&emsp;English</td>
<td>&emsp;&emsp;{English}</td>
<td>&emsp;&emsp;{'Pass' if English > 50 else 'Fail'}</td>
</tr>
<tr>
<td>&emsp;&emsp;3</td>
<td>&emsp;&emsp;Maths</td>
<td>&emsp;&emsp;{Maths}</td>
<td>&emsp;&emsp;{'Pass' if Maths > 50 else 'Fail'}</td>
</tr>
<tr>

R.Kavibharath
1901102
<td>&emsp;&emsp;4</td>
<td>&emsp;&emsp;Physics</td>
<td>&emsp;&emsp;{Physics}</td>
<td>&emsp;&emsp;{'Pass' if Physics > 50 else 'Fail'}</td>
</tr>
<tr>
<td>&emsp;&emsp;5</td>
<td>&emsp;&emsp;Chemistry</td>
<td>&emsp;&emsp;{Chemistry}</td>
<td>&emsp;&emsp;{'Pass' if Chemistry > 50 else 'Fail'}</td>
</tr>
<tr>
<td>&emsp;&emsp;6</td>
<td>&emsp;&emsp;Computer</td>
<td>&emsp;&emsp;{Computer}</td>
<td>&emsp;&emsp;{'Pass' if Computer > 50 else 'Fail'}</td>
</tr>
</table><br><br>
<table>
<tr>
<th>Result</th>
<th>&emsp;&emsp;{'Pass' if Language > 50 and English > 50 and Maths > 50 and Physics >
50 and Chemistry > 50 and Computer > 50 else 'Fail'}
</tr>
</table>
</center>
</body>
</html>
''')

R.Kavibharath
1901102
HTML Code:
<html>
<body>
<form action="/cgi-bin/cgidb.py" method="get">
Roll number: <input type="text" name="roll" /><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>

Output:
HTML Form:

Result Page:

Result:
Thus, python-based CGI script for result publishing was created and executed
successfully.

R.Kavibharath
1901102

You might also like