Found 10419 Articles for Python

How to retrieve cookies in Python CGI Programming?

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

945 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

779 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

548 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

336 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

343 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

239 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 29-May-2025 10:37:14

1K+ 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. Flattening can be performed using different techniques like nested for loops, list comprehensions, list concatenation, and using built-in functions. In this article, we will discuss a few techniques to flatten a shallow Python list. Flattening a shallow list using a nested for loop By using a nested for loop and a 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 example ... Read More

How to clone or copy a list in Python?

Gireesha Devara
Updated on 29-May-2025 10:40:20

2K+ Views

The list in Python is a sequence data type that 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, the assignment operator doesn’t create a new object; rather, it gives another name to an 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 ... Read More

How can I convert a Python tuple to string?

Gireesha Devara
Updated on 29-May-2025 10:44:05

10K+ Views

A tuple is a collection of objects that is ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are that tuples cannot be changed, unlike lists, and tuples use parentheses, whereas lists use square brackets. Converting a Python tuple to a StringThere are three different ways we can convert a Python tuple to a string. Using a for loop. Using the Python join() method ... Read More

Advertisements