Found 27104 Articles for Server Side Programming

How to pass Checkbox Data to Python CGI script?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:17:52

372 Views

Passing Checkbox Data to CGI ProgramCheckboxes are used when more than one option is required to be selected.Here is example HTML code for a form with two checkboxes − Maths Physics The result of this code is the following form −Maths  Physics Select SubjectBelow is checkbox.cgi script to handle input given by web browser for checkbox button.#!/usr/bin/python # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields if form.getvalue('maths'):    math_flag = "ON" else:    math_flag = "OFF" if form.getvalue('physics'):    physics_flag = "ON" else: ... Read More

How to process a simple form data using Python CGI script?

Arnab Chakraborty
Updated on 09-Sep-2023 23:02:23

3K+ Views

Suppose there is an HTML file as below − FirstName: LastName: After submitting this form it should go to a Python page named "getData.py", where you should fetch the data from this HTML page and show. then below is the code for Python CGI. #!C:\Python27\python.exe # Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from fields first_name = form.getvalue('first_name') last_name  = form.getvalue('last_name') print("Content-type:text/html") print print("") print("") print("Hello - Second CGI Program") print("") print("") print(" ... Read More

How to read all HTTP headers in Python CGI script?

harsh manvar
Updated on 27-Feb-2020 05:31:58

981 Views

It is possible to get a custom request header's value in an apache CGI script with python. The solution is similar to this.Apache's mod_cgi will set environment variables for each HTTP request header received, the variables set in this manner will all have an HTTP_ prefix, so for example x-client-version: 1.2.3 will be available as variable HTTP_X_CLIENT_VERSION.So, to read the above custom header just call os.environ["HTTP_X_CLIENT_VERSION"].The below script will print all HTTP_* headers and values −#!/usr/bin/env python import os print "Content-Type: text/html" print "Cache-Control: no-cache" print print "" for headername, headervalue in os.environ.iteritems():     if headername.startswith("HTTP_"):         print "{0} = {1}".format(headername, headervalue)   ... Read More

How to write Python CGI program to interact with MySQL?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:22

895 Views

suppose you want to login into you account using Python CGi script, below is the details login.html email: password: login.py #!C:\Python27\python.exe import MySQLdb import cgi import Cookie # Open database connection db = MySQLdb.connect("localhost", "root", "", "student" ) # prepare a ... Read More

How to execute Python CGI Script on Apache Server?

Arnab Chakraborty
Updated on 30-Jul-2019 22:30:22

412 Views

in apache server normally python script will not run. SO you have to go httpd.conf file in apache server, inside that you will find some .php, .asp etc in a property called AddHandler, you have to put there .py. save the file and restart the server. then run your python CGI script, it will run properly 

How to configure Apache for Python CGI Programming?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:45:17

2K+ Views

Configure Apache Web server for CGITo get your server run CGI scripts properly, you have to configure your Web server. We will discuss how to configure your Apache web server to run CGI scripts.Using ScriptAliasYou may set a directory as ScriptAlias Directive (options for configuring Apache). This way, Apache understands that all the files residing within that directory are CGI scripts. This may be the most simple way to run CGI Scripts on Apache. A typical ScriptAlias line looks like following in httpd.conf file of your Apache web server.ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/So, search ScriptAlias in your httpd.conf file and uncomment the ... Read More

How do we do a file upload using Python CGI Programming?

Rajendra Dharmkar
Updated on 09-Sep-2023 23:04:18

2K+ Views

To upload a file, the HTML form must have the enctype attribute set to multipart/form-data. The input tag with the file type creates a "Browse" button.Example        File:         OutputThe result of this code is the following form −File: Choose file UploadHere is the script save_file.py to handle file upload −#!/usr/bin/python import cgi, os import cgitb; cgitb.enable() form = cgi.FieldStorage() # Get filename here. fileitem = form['filename'] # Test if the file was uploaded if fileitem.filename:    # strip leading path from file name to avoid    # directory traversal attacks   ... Read More

How to retrieve cookies in Python CGI Programming?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:32:59

713 Views

Retrieving CookiesIt is very easy to retrieve all the set cookies. Cookies are stored in CGI environment variable HTTP_COOKIE and they will have following form −key1 = value1;key2 = value2;key3 = value3....Here is an example of how to retrieve cookies.#!/usr/bin/python # Import modules for CGI handling from os import environ import cgi, cgitb if environ.has_key('HTTP_COOKIE'):    for cookie in map(strip, split(environ['HTTP_COOKIE'], ';')):       (key, value ) = split(cookie, '=');       if key == "UserID":          user_id = value       if key == "Password":          password = value print ... Read More

How to setup cookies in Python CGI Programming?

Rajendra Dharmkar
Updated on 30-Aug-2019 06:06:48

554 Views

Setting up CookiesIt is very easy to send cookies to browser. These cookies are sent along with HTTP Header before to Content-type field. Assuming you want to set UserID and Password as cookies. Setting the cookies is done as follows −#!/usr/bin/python print "Set-Cookie:UserID = XYZ;\r" print "Set-Cookie:Password = XYZ123;\r" print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT;\r" print "Set-Cookie:Domain = www.tutorialspoint.com;\r" print "Set-Cookie:Path = /perl;" print "Content-type:text/html\r\r" ...........Rest of the HTML Content....From this example, you must have understood how to set cookies. We use Set-Cookie HTTP header to set cookies.It is optional to set cookies attributes like Expires, Domain, and Path. ... Read More

How do cookies work in Python CGI Programming?

Rajendra Dharmkar
Updated on 30-Jul-2019 22:30:21

394 Views

Using Cookies in CGIHTTP protocol is a stateless protocol. For a commercial website, it is required to maintain session information among different pages. For example, one user registration ends after completing many pages. How to maintain user's session information across all the web pages?In many situations, using cookies is the most efficient method of remembering and tracking preferences, purchases, commissions, and other information required for better visitor experience or site statistics.How Cookies workYour server sends some data to the visitor's browser in the form of a cookie. The browser may accept the cookie. If it does, it is stored as ... Read More

Advertisements