Python Core



Admission Enquiry Form


Server Side Python Script in XAMPP Server

Following are the steps to execute python code in XAMPP server:

Step1 : You should have python installed in your machine.

Step2: Download XAMPP Server in your machine.

Step3: Make changes into httpd.config file of apache server in XAMPP control panel as following:

find addHandler and suffix .py

Step4: Go to the XAMPP Folder then htdocs and create your own folder like compuhelp and create demo.py file to write python code as the following :





Code of Python file demo.py in server side:

#!C:\Python39\python.exe
print("Content-type:text/html\r\n")
num1=10
num2=20
res=num1+num2
print("<h1>Welcome you in python server side" + str(res) + "</h1>")

 




Code of HTML file user-login.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>User Login</title>

</head>
<body>
<div style="background-color:grey ; width: 400px; height: 200px;">
<h1 style="background-color:black; color:whitesmoke; text-align: center;">User Login:</h1>
<form action="user-login.py" method="post" onsubmit="return validate_data()">
<label>Enter User Name:</label><input type="text" name="txt_user"/></br>
</br>
<label>Enter Passowrd :</label><input type="password" name="txt_pass"/></br></br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input style="width:150px;" type="submit" value="Login"/>
</form>
</div>

</body>
</html>

 




Code of Python file user-login.py:

#!c:\python39\python.exe
print("Content-type:text/html\r\n")
import cgi
data=cgi.FieldStorage()
muser=data.getvalue("txt_user")
mpass=data.getvalue("txt_pass")
print("<p> User name is " + muser + "Password is " + mpass +"</p>")



Code of CheckBoxes and Radio Buttons:

Code of HTML file demo-form-check-boxes.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Check boxes</title>
</head>
<body>
<h1>Posting data of Checkboxes:</h1>
<form method="post" action="demo-form-check-boxes.py">
<fieldset>
<legend>Qualification:</legend>
<input type="checkbox" value="10th" name="chk1"/>10th<br>
<input type="checkbox" value="12th" name="chk2"/>12th<br>
<input type="checkbox" value="Graduation" name="chk3"/>Graduation</br>

</fieldset>
<fieldset>
<legend>Gender:</legend>
<input type="radio" value="Male" name="gender"/>Male</br>
<input type="radio" value="Female" name="gender"/>Female</br>
</fieldset>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

Code of Python file demo-form-check-boxes.py:

#!C:\Program Files\Python310\python
print("Content-type:text/html\r\n")
import cgi
data=cgi.FieldStorage()
if data.getvalue("chk1"):
chk1=data.getvalue("chk1")
else:
chk1=""
if data.getvalue("chk2"):
chk2=data.getvalue("chk2")
else:
chk2=""
if data.getvalue("chk3"):
chk3=data.getvalue("chk3")
else:
chk3=""
print("Qualification is :" + chk1 + " ," + chk2 + "," + chk3)
##########################################################
if data.getvalue("gender"):
gender=data.getvalue("gender")
print("Gender is :" + gender)
else:
print("Please select gender:")



Code of Select Box:

Code of HTML file demo-select.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>use of Select </title>
</head>
<body>
<h1>Use of select </h1>
<form method="post" action="demo-select.py">
<h3>Select any course:</h3>
<select name="courses">
<option value="html">html</option>
<option value="css">css</option>
<option value="javascript">java script</option>
<option value="python">Python</option>
</select>
<br>
<h3>Select multiple courses:</h3>
<select name="multi_courses" multiple="true" size="3">
<option value="html">html</option>
<option value="css">css</option>
<option value="javascript">java script</option>
<option value="python">Python</option>
</select>
<input type="submit" value="Submit"/>
</form>
</body>
</html>

Code of Python file demo-select.py:

#!C:\Python39\python
print("Content-type:text/html\r\n")
import cgi
data=cgi.FieldStorage()
courses=data.getvalue("courses")
print("courses are:" + courses + "<br>")
multi_courses=data.getlist("multi_courses")#it returns list of values
print(multi_courses)
print("<br>")
print("<ol>")
for x in multi_courses:
print("<li>" + x + "</li>")



Code to Upload the File:

Code of HTML file demo-file-upload.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File upload</title>
</head>
<body>
<h2>Uploading file using python</h2>
<form method="post" enctype="multipart/form-data" action="demo-file-upload.py">
<input type="file" name="mfile"/>
<input type="submit" value="Upload file"/>
</form>
</body>
</html>

Code of Python file demo-file-upload.py:

#!c:\python39\python
print("Content-type:text/html\r\n")
import os
import cgi
data=cgi.FieldStorage()

mfile= data['mfile']

print("ok")
if mfile.filename:
    filename = os.path.basename(mfile.filename)
    file=open(filename, 'wb')
    file.write(mfile.file.read())
    print("file uploaded successfully...")