Tutorialspoint.dev

CGI Programming in Python

The Common Gateway Interface (CGI) is a standard for writing programs that can interact through a Web server with a client running a Web browser.

  1. CGI is the standard for programs to interface with HTTP servers.
  2. CGI programming is written dynamically generating webpages that respond to user input or webpages that interact with software on the server

Install apache2 on your system can we we will run ‘hello.py’ on host ‘127.0.0.1’
It is recommended to have basic knowledge of HTML before trying this example.
hello.py

#!/usr/bin/python3
# Importing the 'cgi' module
import cgi
  
  
print("Content-type: text/html ")
print("<html><body>")
print("<h1> Hello Program! </h1>")
# Using the inbuilt methods
  
form = cgi.FieldStorage()
if form.getvalue("name"):
    name = form.getvalue("name")
    print("<h1>Hello" +name+"! Thanks for using my script!</h1><br />")
if form.getvalue("happy"):
    print("<p> Yayy! I'm happy too! </p>")
if form.getvalue("sad"):
    print("<p> Oh no! Why are you sad? </p>")
  
# Using HTML input and forms method
print("<form method='post' action='hello2.py'>")
print("<p>Name: <input type='text' name='name' /></p>")
print("<input type='checkbox' name='happy' /> Happy")
print("<input type='checkbox' name='sad' /> Sad")
print("<input type='submit' value='Submit' />")
print("</form")
print("</body></html>")



This article is attributed to GeeksforGeeks.org

tags:

Python

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter