SCSA1211 Unit-5 Notes
SCSA1211 Unit-5 Notes
SCHOOL OF COMPUTING
Data is very important for any organization to continue its operations. The data may be
related to employees in the organization or the operational data like products information,
raw material prices, sales information, profits and losses. Without data, no organization
will survive. Hence, data is very important and it should never be lost.
To store data, a file or database can be used. A file stores data in the secondary storage
device like hard disk, either in the text format or binary format.
A database represents collection of data. Data is stored in the database. Once the data
is stored in the database, various operations can be performed on the data. For example,
modifying the existing data, deleting the unwanted data, or retrieving the data from the
database and etc. To perform such operations, a database comes with software. This is
called a database management system.
1. Database support
SQL
NoSQL
MONGO DB
MongoDB stores data in JSON-like documents, which makes the database very
flexible and scalable.
Big Data
Content Management and Delivery
Mobile and Social Infrastructure
User Data Management
Data Hub
PyMongo
C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python
-m pip install pymongo
Big Data
Content Management and Delivery
Mobile and Social Infrastructure
User Data Management
Data Hub
Test PyMongo
To test if the installation was successful, or if you already have "pymongo" installed,
create a Python page with the following content:
demo_mongodb_test.py:
import pymongo
SCSA1211- Python Programming
Creating a Database
MongoDB will create the database if it does not exist, and make a connection to it.
Example
Program
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
MongoDB waits until you have created a collection (table), with at least one document
(record) before it actually creates the database (and collection).
To create a collection in MongoDB, use database object and specify the name of the
collection you want to create.
Program
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
MongoDB waits until you have inserted a document before it actually creates the
collection.
The first parameter of the insert_one() method is a dictionary containing the name(s)
and value(s) of each field in the document you want to insert.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
SCSA1211- Python Programming
The first parameter of the insert_many() method is a list containing dictionaries with
the data you want to insert:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mylist = [
{ "name": "Amy", "address": "Apple st 652"},
{ "name": "Hannah", "address": "Mountain 21"},
{ "name": "Michael", "address": "Valley 345"},
{ "name": "Sandy", "address": "Ocean blvd 2"},
{ "name": "Betty", "address": "Green Grass 1"},
{ "name": "Richard", "address": "Sky st 331"},
{ "name": "Susan", "address": "One way 98"},
{ "name": "Vicky", "address": "Yellow Garden 2"},
{ "name": "Ben", "address": "Park Lane 38"},
{ "name": "William", "address": "Central st 954"},
SCSA1211- Python Programming
x = mycol.insert_many(mylist)
In MongoDB we use the find and findOne methods to find data in a collection.
Just like the SELECT statement is used to find data in a table in a MySQL
database.
Find One
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
x = mycol.find_one()
SCSA1211- Python Programming
print(x)
Output
To select data from a table in MongoDB, we can also use the find() method.
The first parameter of the find() method is a query object. In this example we use an
empty query object, which selects all documents in the collection.
Example
Return all documents in the "customers" collection, and print each document:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
for x in mycol.find():
print(x)
SCSA1211- Python Programming
When finding documents in a collection, you can filter the result by using a
queryobject.
The first argument of the find() method is a query object, and is used to
limit thesearch.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
SCSA1211- Python Programming
mycol = mydb["customers"]
myquery = { "address": "Park Lane 38" }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
output
Example
Find documents where the address starts with the letter "S" or higher:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myquery = { "address": { "$gt": "S" } }
mydoc = mycol.find(myquery)
for x in mydoc:
print(x)
SCSA1211- Python Programming
Output
The second parameter of the find() method is an object describing which fields to
include in the result.
This parameter is optional, and if omitted, all fields will be included in the result.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
Output
Use the sort() method to sort the result in ascending or descending order.
The sort() method takes one parameter for "fieldname" and one parameter for
"direction" (ascending is the default direction).
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)
OUTPUT
SCSA1211- Python Programming
Sort Descending
sort("name", 1) #ascending
sort("name", -1) #descending
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
SCSA1211- Python Programming
for x in mydoc:
print(x)
Output
The first parameter of the delete_one() method is a query object defining which
document to delete.
Note: If the query finds more than one document, only the first occurrence is deleted.
SCSA1211- Python Programming
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mycol.delete_one(myquery)
The first parameter of the delete_many() method is a query object defining which
documents to delete.
Example
Delete all documents were the address starts with the letter S:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
SCSA1211- Python Programming
x = mycol.delete_many(myquery)
output
2 documents deleted.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
x = mycol.delete_many({})
Output:
11 documents deleted
Delete Collection
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mycol.drop()
The drop() method returns true if the collection was dropped successfully, and false if
the collection does not exist.
SCSA1211- Python Programming
The first parameter of the update_one() method is a query object defining which
document to update.
Note: If the query finds more than one record, only the first occurrence is updated.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mycol.update_one(myquery, newvalues)
OUTPUT
Update Many
To update all documents that meets the criteria of the query, use
the update_many() method.
Example
Update all documents where the address starts with the letter "S":
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
SCSA1211- Python Programming
= mycol.update_many(myquery, newvalues)
Output
2 documents updated.
The limit() method takes one parameter, a number defining how many documents to
return.
Example
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
myresult = mycol.find().limit(5)
OUTPUT
{'_id': 1, 'name': 'John', 'address': 'Highway37'}
{'_id': 2, 'name': 'Peter', 'address': 'Lowstreet 27'}
{'_id': 3, 'name': 'Amy', 'address': 'Apple st 652'}
{'_id': 4, 'name': 'Hannah', 'address': 'Mountain 21'}
{'_id': 5, 'name': 'Michael', 'address': 'Valley 345'}
SCSA1211- Python Programming
CURSOR CLASS
import mysql.connector;
conn=mysql.connector.connect(host=‟localhost‟,database=‟university‟,user=‟root‟,
password=‟***‟)
The next step is to create cursor class object by calling the cursor() method on „conn‟
object as:
cursor=con.cursor()
The resultant rows retirieved from the table are stored in cursor object. theresult
can be fetched using fetchone() or fetchall() methods.
Finally, the connection with MySQL can be closed by closing the cursor andconnection
objects as:
cursor.close()
conn.close()
Program: A python program to retrieve and display all rows from the studenttable:
import mysql.connector;
conn=mysql.connector.connect(host=‟localhost‟,database=‟university‟,user=‟root‟,
password=‟***‟)
cursor=con.cursor()
row = cursor.fetchone()
print(row)
row=cursor.fetchone()
cursor.close()
conn.close()
SCSA1211- Python Programming
Output:
EXCEPTIONS CLASSES
Built in Exceptions
Exception Description
Warning Used for non-fatal issues. Must subclass StandardError.
Error
Base class for errors. Must subclass StandardError.
InterfaceError Used for errors in the database module, not the database
itself. Must subclass Error.
DatabaseError Used for errors in the database. Must subclass Error.
DataError Subclass of DatabaseError that refers to errors in the
data.
OperationalError Subclass of DatabaseError that refers to errors such as the
loss of a connection to the database. These errors are
generally outside of the control of the Python scripter.
SCSA1211- Python Programming
Exception Description
IntegrityError Subclass of DatabaseError for situations that would
damage the relational integrity, such as uniqueness
constraints or foreign keys.
InternalError Subclass of DatabaseError that refers to errors internal to
the database module, such as a cursor no longer being
active.
ProgrammingError Subclass of DatabaseError that refers to errors such as a
bad table name and other things that can safely be blamed
on you.
NETWORKING
For a specific purpose if things are connected together, are referred asa
NETWORK. A network can be of many types, like a telephone network, television
network, computer network or even a people network.
IP : Internet Protocol
FTP : File Transfer Protocol
SMTP : Simple Mail Transfer Protocol
HTTP : Hyper Text Transfer Protocol
The Network reference models were developed to allow products from different
manufacturers to interoperate on a network. A network reference model serves as a
blueprint, detailing standards for how protocol communication should occur. The
most widely recognized reference models are, the Open Systems Interconnect (
OSI ) Model and Department of Defense ( DoD, also known as TCP/IP )
model.
Network Types are often categorized by their size and functionality. According to the
size, the network can be commonly categorized into Three types.
For example:
SCSA1211- Python Programming
In a network, any connected device is called as host. A host can serve as following
ways:
SOCKET MODULE
A socket is a link between two applications that can communicate with one another
(either locally on a single machine or remotely between two machines in separate
locations).
Basically, sockets act as a communication link between two entities, i.e. a server and
a client. A server will give out information being requested by a client. For example,
when you visited this page, the browser created a socket and connected to the server.
SCSA1211- Python Programming
In order to create a socket, you use the socket.socket() function, and the syntax is as
simple as:
import socket
After obtaining your socket object, you can then create a server or client as desired
using the methods available in the socket module.
Before we get started, let's look at the client socket methods available in Python.
s= socket.socket(socket.AF_INET, socket.sock_STREAM)
To create a new socket, you first import the socket method of the socket class.
import socket
The AF_INET argument indicates that you're requesting an Internet Protocol (IP)
socket, specifically IPv4. The second argument is the transport protocol type
SOCK_STREAM for TCP sockets. Additionally, you can also create an IPv6 socket
by specifying the socket AF_INET6 argument.
server = "localhost"
port =80
stream_socket.connect(server_address)
It's important to note that the host and port must be a tuple.
message = 'message'
stream_socket.sendall(message)
data = sock.recv(10)
print data
stream_socket.close()
import socket
import sys
# Define host
host = 'localhost'
SCSA1211- Python Programming
port = 8080
print "connecting"
stream_socket.connect(server_address)
# Send data
message = 'message'
stream_socket.sendall(message)
# response
data = stream_socket.recv(10)
print data
stream_socket.close()
Now let's take a look at a simple Python server. The following are the socket server
methods available in Python.
SCSA1211- Python Programming
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Create a socket.
import socket
import sys
# Define host
host = 'localhost'
port = 8080
SCSA1211- Python Programming
sock.bind((host, port))
sock.listen(1)
data = connection.recv(16)
if data:
connection.sendall(data)
else:
connection.close()
SCSA1211- Python Programming
Now run the client and server programs in separate terminal windows, so they can
communicate with each other.
Server Output
$ python server.py
received "message"
Client Output
$ python client.py
connecting
message
socket closed
Simple Mail Transfer Protocol (SMTP) is a protocol, which handles sending e-mail
and routing e-mail between mail servers.
SCSA1211- Python Programming
Python provides smtplib module, which defines an SMTP client session object that
can be used to send mail to any Internet machine with an SMTP or ESMTP listener
daemon.
Here is a simple syntax to create one SMTP object, which can later be used to send
an e-mail –
import smtplib
host − This is the host running your SMTP server. You can specify IP address
of the host or a domain name like tutorialspoint.com. This is optional
argument.
port − If you are providing host argument, then you need to specify a port,
where SMTP server is listening. Usually this port would be 25.
An SMTP object has an instance method called sendmail, which is typically used to
do the work of mailing a message. It takes three parameters −
Example
Here is a simple way to send one e-mail using Python script. Try it once −
import smtplib
sender = '[email protected]'
receivers = ['[email protected]']
"""
try:
smtpObj = smtplib.SMTP('localhost')
except SMTPException:
SCSA1211- Python Programming
Here, you have placed a basic e-mail in message, using a triple quote, taking care to
format the headers correctly. An e-mail requires a From, To, and Subject
header, separated from the body of the e-mail with a blank line.
To send the mail you use smtpObj to connect to the SMTP server on the local machine and
then use the sendmail method along with the message, the from address, and the
destination address as parameters (even though the from and to addresses are within the e-
mail itself, these aren't always used to route mail).
If you are not running an SMTP server on your local machine, you can use smtplib
client to communicate with a remote SMTP server. Unless you are using a webmail service (such
as Hotmail or Yahoo! Mail), your e-mail provider must have provided you with outgoing mail
server details that you can supply them, as follows −smtplib.SMTP('mail.your-domain.com', 25)
URL ACCESS
urllib is a Python module that can be used for opening URLs. It defines functions and classes to
help in URL actions.
With Python we can also access and retrieve data from the internet like XML, HTML, JSON,
etc. We can also use Python to work with this data directly.
import urllib.request
x= urllib.request.urlopen('https://www.google.com/')
print(x.read())