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
" ) 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>" ) |
leave a comment
0 Comments