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.
- CGI is the standard for programs to interface with HTTP servers.
- 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\r\n\r\n") 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>") |
chevron_right
filter_none
This article is contributed by Harsh Wardhan Chaudhary (Intern). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Recommended Posts:
- How to write an empty function in Python - pass statement?
- Operator Functions in Python | Set 2
- Time Functions in Python | Set-2 (Date Manipulations)
- Send mail from your Gmail account using Python
- Python – The new generation Language
- Print Single and Multiple variable in Python
- Increment and Decrement Operators in Python
- str() vs repr() in Python
- Swap two variables in one line in C/C++, Python, PHP and Java
- Generate all permutation of a set in Python
- Class or Static Variables in Python
- trunc() in Python
- Division Operators in Python
- Interesting facts about strings in Python | Set 1
- When to use yield instead of return in Python?



