CGI Python Programming

Python Methods and Functions

Common Gateway Interface (CGI) — it is a standard for writing programs that can communicate through a web server with a client running in a web browser.

  1. CGI is a standard for programs to communicate with HTTP servers.
  2. CGI programming is written by dynamically generating web pages that respond to user input or web pages that interact with software on the server.

Install apache2 on your system, we we can run hello.py on host 127.0.0.1
It is recommended that you have a basic knowledge of HTML before trying this example. 
hello.py

#! / Usr / bin / python3
# Import module & # 39; cgi & # 39;

import cgi

  

 

print ( "Content-type: text / html" )

print ( "& lt; html & gt; & lt; body & gt;" )

print ( "& lt; h1 & gt; Hello Program! & lt; / h1 & gt;" )

# Using built-in methods

 

form = cgi.FieldStorage ()

if form.getvalue ( "name" ):

name = form.getvalue ( "name" )

print ( " & lt; h1 & gt; Hello " + name + " ! Thanks for using my script! & Lt; / h1 & gt; & lt; br / & gt; " )

if form.getvalue ( "happy" ):

print ( "& lt; p & gt; Yayy! I'm happy too! & lt; / p & gt;" )

if form.getvalue ( "sad" ) :

print ( "& lt; p & gt; Oh no! Why are you sad? & lt; / p & gt;" )

 
# Using HTML input and form method

print ( " & lt; form method = 'post' action =' hello2.py' & gt; " )

print ( "& lt; p & gt; Name: & lt; input type =' text' name = 'name' / & gt; & lt; / p & gt; " )

print ( "& lt; input type =' checkbox' name = 'happy' / & gt; Happy " )

print ( "& lt; input type =' checkbox' name = 'sad' / & gt; Sad" )

print ( "& lt; input type =' submit' value = 'Submit '/ & gt;" )

print ( "& lt; / form" )

print ( "& lt; / body & gt; & lt; / html & gt;" )

This article is courtesy of Harsh Wardhan Chaudhary ( Intern) If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by mail from articles [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.





Tutorials