Unit 5 Lab Programs
Unit 5 Lab Programs
Ex.No 5.1: Connect to the NoSQL database using a Python connector module, such as
"pymongo" for MongoDB or "cassandra-driver" for Cassandra.
Steps to be followed:
1. https://www.mongodb.com/try/download/community
7.
8.
9.Open MongoCompass
10.Click Connect
Algorithm:
Program:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
print(client)
alldb=client.list_database_names()
print(alldb)
Output:
Ex.No:5.2: Use a cursor to iterate over the records in a collection/table and
print specific fields/attributes
Aim: Use a cursor to iterate over the records in a collection/table and print
specific fields/attributes
Algorithm:
Step 1.Import pymongo
Step 2.Connect with Mongocompas using MongoClient command
Step 3.Create a ‘Employee’ data base
Step 4.Create a collection ‘Information’
Step 5.Create a document rec with attributes
{EMPNO,ENAME,JOB,HIREDATE,SAL,DEPTNO}
Step 6.Finally insert the document into the collection using Insert_one or
insert_many command.
Step 7.Open the Mongodb .The created collection will be displayed.
Program:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
rec={
"EMPNO" : 7934,
"ENAME" : "AAAAA",
"JOB" : "CLERK",
"HIREDATE" : "1.08.2018",
"SAL" : 35000,
"DEPTNO" : 10
doc=information.insert_one(rec)
print(doc)
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
rec=[{
"EMPNO" : 7934,
"ENAME" : "AAAAA",
"JOB" : "CLERK",
"HIREDATE" : "1.08.2018",
"SAL" : 35000,
"DEPTNO" : 10
},
"EMPNO" : 7935,
"ENAME" : "BBBB",
"JOB" : "CODER",
"HIREDATE" : "1.08.2019",
"SAL" : 55000,
"DEPTNO" : 5
},
"EMPNO" : 7936,
"ENAME" : "CCCC",
"JOB" : "MANAGER",
"HIREDATE" : "1.08.2008",
"SAL" : 100000,
"DEPTNO" : 1 }
doc=information.insert_many(rec)
print(doc)
Output:
//find the collection
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
print(read)
Output:
import pymongo
client=pymongo.MongoClient('mongodb://localhost:27017')
mydb=client['Employee']
information=mydb.table
dis=information.find()
print(record)
O/P:
Exp:5.3: Implement error handling for specific scenarios, such as duplicate key
violation or record not found, in the NoSQL database.
Aim: Implement error handling for specific scenarios, such as duplicate key
violation or record not found, in the MongoDB.
Algorithm:
o Import MongoClient and errors from pymongo for database connection and
error handling.
Program:
from pymongo import MongoClient, errors
# Connect to MongoDB
client = MongoClient("mongodb://localhost:27017")
db = client["Company"]
collection = db["Employees"]
except errors.DuplicateKeyError:
print("Error: Duplicate key violation. Employee ID must be
unique.")
if result:
print("Employee Found:", result)
else:
raise ValueError("Record not found.")
except ValueError as e:
print(f"Error: {e}")
except errors.OperationFailure as e:
print(f"MongoDB Operation Error: {e}")
except errors.PyMongoError as e:
print(f"General MongoDB Error: {e}")
finally:
client.close() # Close the connection
Output:
Error: Duplicate key violation. Employee ID must be unique.
Error: Record not found.
Ex.No:5.4
Implement a TCP/IP client-server application using the socket module for sending and
receiving messages.
Aim: Implement aTCP/IP client-server application using the socket module for sending
and receiving messages.
# Server settings
HOST = '127.0.0.1' # Localhost (use '0.0.0.0' to accept connections from any IP)
PORT = 12345 # Port number (must match client)
while True:
client_socket, client_address = server_socket.accept() # Accept a connection
print(f"Connection from {client_address}")
# Send response
response = f"Server received: {data}"
client_socket.send(response.encode('utf-8'))
Client Program
import socket
# Send message
message = "Hello, Server!"
client_socket.send(message.encode('utf-8'))
# Receive response
response = client_socket.recv(1024).decode('utf-8')
print(f"Received from server: {response}")
Output:
Output:
Aim: To write a python program using the SMTPLIB module to send an email from a specified
email address to another recipient.
Algorithm:
Program:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
# Email Configuration
SMTP_SERVER = "smtp.gmail.com" # Change for Yahoo, Outlook, etc.
SMTP_PORT = 587 # Port for TLS
# Email Content
subject = "Test Email from Python"
body = "Hello,\n\nThis is a test email sent using Python and smtplib.\n\nBest Regards,\nYour
Name"
try:
# Connect to SMTP Server
server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
server.starttls() # Secure connection using TLS
server.login(SENDER_EMAIL, SENDER_PASSWORD) # Login to email account
# Send Email
server.sendmail(SENDER_EMAIL, RECIPIENT_EMAIL, msg.as_string())
except Exception as e:
print(f"✅Error: {e}")
finally:
server.quit() # Close the connection
2. Give valid sender and receiver mail ids and run the python program
Output: