Found 13 Articles for CGI

How to send the result of Python CGI script to the browser?

Arnab Chakraborty
Updated on 22-Jun-2020 15:37:06

175 Views

# Get data from fields from HTML page first_name = form.getvalue('first_name') last_name  = form.getvalue('last_name') send data to Browser print("Content-type:text/html") print print("") print("") print("Hello - Second CGI Program") print("") print("") print(" Hello %s %s " % (first_name, last_name)) print("") print("")

How to read all HTTP headers in Python CGI script?

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

910 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

796 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

376 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

649 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

504 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

378 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

What is the difference between GET and POST in Python CGI Programming?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:31:23

1K+ Views

GET and POST MethodsYou must have come across many situations when you need to pass some information from your browser to web server and ultimately to your CGI Program. Most frequently, browser uses two methods two pass this information to web server. These methods are GET Method and POST Method.Passing Information using GET methodThe GET method sends the encoded user information appended to the page request. The page and the encoded information are separated by the ? character as follows −http://www.test.com/cgi-bin/hello.py?key1=value1&key2=value2The GET method is the default method to pass information from browser to web server and it produces a long ... Read More

Advertisements