0% found this document useful (0 votes)
42 views

Output

The document provides code samples for a server-client socket program to exchange messages between the two, and a Python script to send an email using SMTP. The socket program code defines a server and client that can continuously send and receive strings. The email code uses the smtplib and email modules to log into a Gmail SMTP server, compose an email, and send it to a recipient-provided address.

Uploaded by

lawen94360
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Output

The document provides code samples for a server-client socket program to exchange messages between the two, and a Python script to send an email using SMTP. The socket program code defines a server and client that can continuously send and receive strings. The email code uses the smtplib and email modules to log into a Gmail SMTP server, compose an email, and send it to a recipient-provided address.

Uploaded by

lawen94360
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Practical 10

1. Write a socket program to exchange two way information between server and
client.
Program:
Server code:
import socket
host = 'localhost'
port = 5000
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.bind((host, port))
soc.listen(1)
conn, address = soc.accept()
print("Client is connected.")
while True:
message = conn.recv(1024).decode()
if not message:
break
print("Client says:",message)
response = input("Enter your response: ")
conn.send(response.encode())
soc.close()

Client code:
import socket
host = 'localhost'
port = 5000
soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
soc.connect((host, port))
print("Connection with server established.")
message = input("Enter message: ")
while message != "exit":
soc.send(message.encode())
response = soc.recv(1024).decode()
print("Server says:",response)
message = input("Enter your response: ")
soc.close()

Error:

Output:
2. Send email using SMTP.
Program:
import smtplib
from email.mime.text import MIMEText
import os
fromAddress = '[email protected]'
to = input("Enter the email address of the recipient: ")
subject = input("Enter the subject of the email: ")
message = input("Enter the message of the email: ")
msg = MIMEText(message)
msg['Subject'] = subject
msg['To'] = to
msg['From'] = fromAddress
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(fromAddress, os.getenv('EMAIL_PASSWORD'))
server.send_message(msg)
print("Email sent successfully")
server.quit()

Error:

Output:

Email received:

You might also like