Programming Articles - Page 2110 of 3363

Database Connection in Python

Mohd Mohtashim
Updated on 31-Jan-2020 09:45:11

712 Views

Before connecting to a MySQL database, make sure of the followings −You have created a database TESTDB.You have created a table EMPLOYEE in TESTDB.This table has fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.User ID "testuser" and password "test123" are set to access TESTDB.Python module MySQLdb is installed properly on your machine.You have gone through MySQL tutorial to understand MySQL Basics.ExampleFollowing is the example of connecting with MySQL database "TESTDB"#!/usr/bin/python import MySQLdb # Open database connection db = MySQLdb.connect("localhost", "testuser", "test123", "TESTDB" ) # prepare a cursor object using cursor() method cursor = db.cursor() # execute SQL query using execute() method. ... Read More

How do I Install MySQLdb in Python?

Mohd Mohtashim
Updated on 31-Jan-2020 09:43:48

3K+ Views

Before proceeding, you make sure you have MySQLdb installed on your machine. Just type the following in your Python script and execute it −#!/usr/bin/python import MySQLdbIf it produces the following result, then it means MySQLdb module is not installed −Traceback (most recent call last):    File "test.py", line 3, in       import MySQLdb ImportError: No module named MySQLdbTo install MySQLdb module, use the following command −For Ubuntu, use the following command - $ sudo apt-get install python-pip python-dev libmysqlclient-dev For Fedora, use the following command - $ sudo dnf install python python-devel mysql-devel redhat-rpm-config gcc For Python ... Read More

How To Raise a "File Download" Dialog Box in Python?

Mohd Mohtashim
Updated on 31-Jan-2020 09:43:05

577 Views

Sometimes, it is desired that you want to give option where a user can click a link and it will pop up a "File Download" dialogue box to the user instead of displaying actual content. This is very easy and can be achieved through HTTP header. This HTTP header is be different from the header mentioned in previous section.For example, if you want make a FileName file downloadable from a given link, then its syntax is as follows −#!/usr/bin/python # HTTP Header print "Content-Type:application/octet-stream; name = \"FileName\"\r"; print "Content-Disposition: attachment; filename = \"FileName\"\r"; # Actual File Content will go here. ... Read More

File Upload Example in Python

SaiKrishna Tavva
Updated on 23-Sep-2024 14:19:21

10K+ Views

There are two common ways to upload a file using Python. One is through a cloud storage service using a web server, and CGI environment, (also known as an automated file upload system). In this tutorial, we will focus on file uploading using the CGI (Common Gateway Interface) environment. The process involves generating an HTML form for file uploading and a Python script to manage file saving and uploading to the server. The steps involved in uploading files using Python are as follows - Creating HTML Form ... Read More

Setting up Cookies in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:08:10

2K+ Views

It 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 −Example#!/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. It is ... Read More

Using Cookies in CGI in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:07:20

636 Views

HTTP 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 It Works?Your 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 a plain text ... Read More

Passing Drop Down Box Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:06:27

529 Views

Drop Down Box is used when we have many options available but only one or two will be selected.ExampleHere is example HTML code for a form with one drop down box − Maths Physics OutputThe result of this code is the following form −Below is dropdown.py script to handle input given by web browser.#!/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('dropdown'):    subject = form.getvalue('dropdown') else:    subject = "Not entered" print "Content-type:text/html\r\r" print "" print "" print "Dropdown Box ... Read More

Passing Text Area Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:04:50

332 Views

TEXTAREA element is used when multiline text has to be passed to the CGI Program.ExampleHere is example HTML code for a form with a TEXTAREA box − Type your text here... The result of this code is the following form −Below is textarea.cgi script to handle input given by web browser −#!/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('textcontent'):    text_content = form.getvalue('textcontent') else:    text_content = "Not entered" print "Content-type:text/html\r\r" print "" print ""; print "Text Area - Fifth ... Read More

Passing Radio Button Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:05:24

373 Views

Radio Buttons are used when only one option is required to be selected.ExampleHere is example HTML code for a form with two radio buttons − Maths Physics The result of this code is the following form −Below is radiobutton.py script to handle input given by web browser for radio 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('subject'):    subject = form.getvalue('subject') else:    subject = "Not set" print "Content-type:text/html\r\r" print "" print "" print "Radio - Fourth CGI Program" ... Read More

Passing Checkbox Data to CGI Program in Python

Mohd Mohtashim
Updated on 31-Jan-2020 08:02:13

631 Views

Checkboxes are used when more than one option is required to be selected.ExampleHere is example HTML code for a form with two checkboxes − Maths Physics OutputThe result of this code is the following form −Below 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:    physics_flag = "OFF" print "Content-type:text/html\r\r" print "" ... Read More

Advertisements