Found 10784 Articles for Python

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

727 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

558 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

405 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

2K+ 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

What are important HTTP headers to be frequently used in Python CGI Programming?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:25:28

267 Views

HTTP HeaderThe line Content-type:text/html\r\r is part of HTTP header which is sent to the browser to understand the content. All the HTTP header will be in the following form −HTTP Field Name − Field ContentFor ExampleContent-type − text/html\r\rThere are few other important HTTP headers, which we will use frequently in our CGI Programming.     Sr.No.HeaderDescription1Content-type:A MIME string defining the format of the file being returned. Example is Content-type:text/html2Expires: DateThe date the information becomes invalid. It is used by the browser to decide when a page needs to be refreshed. A valid date string is in the format 01 Jan 1998 ... Read More

What Content-type is required to write Python CGI program?

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

270 Views

If we run simple scripts like hello.py, its output is written on the STDOUT file, i.e., screen. There is one important and extra feature available which is the first line to be printed Content-type:text/html\r\r. This line is sent back to the browser and it specifies the content type to be displayed on the browser screen.We can write advanced CGI programs using Python. This script can interact with any other external system and even exchange information with RDBMS.

What are the modules required for CGI programming in Python?

Rajendra Dharmkar
Updated on 16-Jun-2020 12:21:40

151 Views

Python's cgi module is usually the starting place in writing CGI programs in Python. The main purpose of the cgi module is to extract the values passed to a CGI program from an HTML form. Mostly one interacts with CGI applications through an HTML form. One fills out some values in the form that specify details of the action to be performed, then call on the CGI to perform its action using your specifications.You may include many input fields within an HTML form which can be of a number of different types (text, checkboxes, picklists, radio buttons, etc.).Your Python script ... Read More

How to flatten a shallow list in Python?

Gireesha Devara
Updated on 24-Aug-2023 14:26:23

942 Views

Flattening a shallow list means converting a nested list into a simple single dimensional list. In other words, converting a multidimensional list into a one-dimensional list. The process of flattening can be performed by using different techniques like nested for loops, list comprehensions, list concatenation, and by using built-in functions. In this article, we will discuss a few techniques to flatten a shallow python list. Using nested for loop By using nested for loop and list.append() method we can flatten the shallow list. Let’s have a look and see how this can be done in a program. Example This simple ... Read More

How to clone or copy a list in Python?

Gireesha Devara
Updated on 24-Aug-2023 13:32:04

821 Views

The list in Python is a sequence data type which is used to store various types of data. A list is created by placing each data element inside square brackets [] and these are separated by commas. In Python, assignment operator doesn’t create a new object, rather it gives another name to already existing object. This can be verified by id() function >>> L1 = [1, 2, 3, 4] >>> L2 = L1 >>> id(L1) 185117137928 >>> id(L2) 185117137928 There are various ways of cloning/copying a list in python. In this article, we will discuss some of them. Using ... Read More

Advertisements