SlideShare a Scribd company logo
Chapter - 5
Network – 5.2
Maulik Borsaniya
Gardividyapith
What is network ?
• A computer network, or data network, is a digital
telecommunications network which allows
nodes to share resources. In computer networks,
computing devices exchange data with each
other using connections between nodes
What is protocol ?
• A network protocol defines rules and conventions for
communication between network devices.
• Network protocols include mechanisms for devices to
identify and make connections with each other, as well
as formatting rules that specify how data is packaged
into messages sent and received
• FTP: File Transfer Protocol. ...
• SSL: Secure Sockets Layer. ...
• TELNET (telnet) ...
• SMTP: Simple Mail Transfer Protocol. ...
• POP3: Post Office Protocol.
Starting With the Socket Programming
• Socket programming is a way of connecting two nodes on a
network to communicate with each other. One socket(node)
listens on a particular port at an IP, while other socket reaches
out to the other to form a connection.
• Server forms the listener socket while client reaches out to
the server.
• They are the real backbones behind web browsing. In simpler
terms there is a server and a client.
• Socket programming is started by importing the socket library
and making a simple socket
• import socket s =socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
• Here we made a socket instance and passed it
two parameters.
• The first parameter is AF_INET and the
• second one is SOCK_STREAM.
• AF_INET refers to the address family ipv4.
• The SOCK_STREAM means connection oriented
TCP protocol.
Here is an example of a script for connecting to Google
An example script to connect to Google using socket
# programming in Python
import socket # for socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print ("Socket successfully created")
except socket.error as err:
print ("socket creation failed with error")
# default port for socket
port = 80
try:
host_ip = socket.gethostbyname('www.google.com')
except socket.gaierror:
# this means could not resolve the host
print ("there was an error resolving the host")
sys.exit()
# connecting to the server
s.connect((host_ip, port))
print ("the socket has successfully connected to google on port == %s" %(host_ip))
Http Request Reponses
• HTTP is based on the client-server architecture model and a
stateless request/response protocol that operates by
exchanging messages across a reliable TCP/IP connection.
• An HTTP "client" is a program (Web browser or any other
client) that establishes a connection to a server for the
purpose of sending one or more HTTP request messages. An
HTTP "server" is a program ( generally a web server like
Apache Web Server or Internet Information Services IIS, etc. )
• that accepts connections in order to serve HTTP requests by
sending HTTP response messages.
Installation of http requests
Navigate  Python34/script
Pip install requests
Download images from web – with
request response
import requests
image_url = "https://www.python.org/static/community_logos/python-logo-master-v3-
TM.png"
# URL of the image to be downloaded is defined as image_url
r = requests.get(image_url) # create HTTP response object
# send a HTTP request to the server and save
# the HTTP response in a response object called r
with open("python_logo.png",'wb') as f:
# Saving received content as a png file in
# binary format
# write the contents of the response (r.content)
# to a new file in binary mode.
f.write(r.content)
Client Server Connection
Establishment
#Server
# first of all import the socket library
import socket
# next create a socket object
s = socket.socket()
print ("Socket successfully created")
# reserve a port on your computer in our
# case it is 12345 but it can be anything
port = 12345
# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests
# coming from other computers on the network
s.bind(('', port))
print ("socket binded to %s" %(port))
# put the socket into listening mode
s.listen(5)
print ("socket is listening")
# a forever loop until we interrupt it or
# an error occurs
while True:
# Establish connection with client.
c, addr = s.accept()
print ('Got connection from', addr)
# send a thank you message to the client.
c.send('Thank you for connecting')
# Close the connection with the client
Python server.py
telnet localhost 12345
A server has a bind() method which binds it to a
specific ip and port so that it can listen to
incoming requests on that ip and port.
A server has a listen() method which puts the
server into listen mode. This allows the server to
listen to incoming connections. And last a server
has an accept() and close() method.
The accept method initiates a connection with the
client and the close method closes the connection
with the client.
• First of all we import socket which is necessary.
• Then we made a socket object and reserved a port on our pc.
• After that we binded our server to the specified port. Passing
an empty string means that the server can listen to incoming
connections from other computers as well. If we would have
passed 127.0.0.1 then it would have listened to only those calls
made within the local computer.
• After that we put the server into listen mode.5 here means
that 5 connections are kept waiting if the server is busy and if
a 6th socket try's to connect then the connection is refused.
• At last we make a while loop and start to accept all incoming
connections and close those connections after a thank you
message to all connected sockets.
#Client
# Import socket module
import socket
# Create a socket object
s = socket.socket()
# Define the port on which you want to connect
port = 12345
# connect to the server on local computer
s.connect(('127.0.0.1', port))
# receive data from the server
s.recv(1024)
# close the connection
s.close()
TCP & UDP
• The user datagram protocol (UDP) works differently
from TCP/IP.
• Where TCP is a stream oriented protocol, ensuring that
all of the data is transmitted in the right order,
• UDP is a message oriented protocol.
• UDP does not require a long-lived connection, so setting
up a UDP socket is a little simpler.
• On the other hand, UDP messages must fit within a
single packet (for IPv4, that means they can only hold
65,507 bytes because the 65,535 byte packet also
includes header information) and delivery is not
guaranteed as it is with TCP.
Sending mail
# Python code to illustrate Sending mail from
#error
# your Gmail account
import smtplib
# creates SMTP session
s = smtplib.SMTP('smtp.gmail.com', 587)
# start TLS for security
s.starttls()
# Authentication
s.login("patelmaulik752@gmail.com", “password")
# message to be sent
message = "Test Message"
# sending the mail
s.sendmail("borsaniyamaulik@gmail.com", "inspirationalstory1@gmail.com", message)
# terminating the session
s.quit()
success
import smtplib
gmailaddress = input("what is your gmail address? n ")
gmailpassword = input("what is the password for that
email address? n ")
mailto = input("what email address do you want to send
your message to? n ")
msg = input("What is your message? n ")
mailServer = smtplib.SMTP('smtp.gmail.com' , 587)
mailServer.starttls()
mailServer.login(gmailaddress , gmailpassword)
mailServer.sendmail(gmailaddress, mailto , msg)
print(" n Sent!")
mailServer.quit()

More Related Content

PPTX
Socket programming in Java (PPTX)
PPTX
Java socket programming
PDF
Java sockets
PDF
Socket Programming
PPT
Socket programming-tutorial-sk
DOC
socket programming
PPT
Networking & Socket Programming In Java
PDF
Socket programming using java
Socket programming in Java (PPTX)
Java socket programming
Java sockets
Socket Programming
Socket programming-tutorial-sk
socket programming
Networking & Socket Programming In Java
Socket programming using java

What's hot (20)

PPT
Application Layer and Socket Programming
PPT
Network programming in Java
PPT
A Short Java Socket Tutorial
PPT
Networking Java Socket Programming
PPT
Basic socket programming
PPT
Socket Programming
PDF
Network programming Using Python
PPTX
Elementary TCP Sockets
PPT
Networking in java
PPTX
Python Sockets
PPTX
Tcp/ip server sockets
PPT
Tcp sockets
PPTX
Network programming in java - PPT
PPTX
PPT
Java API: java.net.InetAddress
PPT
Socket programming
PDF
Socket programming-in-python
PPT
java networking
PPTX
Advance Java-Network Programming
PDF
Java networking programs socket based
Application Layer and Socket Programming
Network programming in Java
A Short Java Socket Tutorial
Networking Java Socket Programming
Basic socket programming
Socket Programming
Network programming Using Python
Elementary TCP Sockets
Networking in java
Python Sockets
Tcp/ip server sockets
Tcp sockets
Network programming in java - PPT
Java API: java.net.InetAddress
Socket programming
Socket programming-in-python
java networking
Advance Java-Network Programming
Java networking programs socket based
Ad

Similar to PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA (20)

PPT
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
PPTX
python programming
PPT
Unit 8 Java
PPTX
Socket programming
PPT
Network programming in Java
PPTX
Networking.pptx
PPTX
#1 (TCPvs. UDP)
PPT
Sockets
PPTX
5_6278455688045789623.pptx
PPT
Network Programming in Java
PPT
Socket Programming - nitish nagar
PPT
Md13 networking
PPT
Lan chat system
PPTX
PPTX
PPT
Networking
PDF
Java networking programs - theory
DOCX
Mail Server Project Report
PPTX
Client server chat application
PPTX
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
Sockets.ppt socket sofcv ohghjagshsdjjhjfb
python programming
Unit 8 Java
Socket programming
Network programming in Java
Networking.pptx
#1 (TCPvs. UDP)
Sockets
5_6278455688045789623.pptx
Network Programming in Java
Socket Programming - nitish nagar
Md13 networking
Lan chat system
Networking
Java networking programs - theory
Mail Server Project Report
Client server chat application
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
Ad

More from Maulik Borsaniya (7)

PPTX
Dragon fruit-nutrition-facts
PPTX
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
PPTX
PYTHON - EXTRA Chapter GUI - MAULIK BORSANIYA
PPTX
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
PPTX
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA
Dragon fruit-nutrition-facts
Chapter 5 - THREADING & REGULAR exp - MAULIK BORSANIYA
PYTHON - EXTRA Chapter GUI - MAULIK BORSANIYA
PYTHON-Chapter 4-Plotting and Data Science PyLab - MAULIK BORSANIYA
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PYTHON -Chapter 2 - Functions, Exception, Modules and Files -MAULIK BOR...
Chapter 1 - INTRODUCTION TO PYTHON -MAULIK BORSANIYA

Recently uploaded (20)

PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
Cloud computing and distributed systems.
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Modernizing your data center with Dell and AMD
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
A Presentation on Artificial Intelligence
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
20250228 LYD VKU AI Blended-Learning.pptx
Cloud computing and distributed systems.
NewMind AI Monthly Chronicles - July 2025
NewMind AI Weekly Chronicles - August'25 Week I
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Network Security Unit 5.pdf for BCA BBA.
The Rise and Fall of 3GPP – Time for a Sabbatical?
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
“AI and Expert System Decision Support & Business Intelligence Systems”
Modernizing your data center with Dell and AMD
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf
cuic standard and advanced reporting.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
A Presentation on Artificial Intelligence
The AUB Centre for AI in Media Proposal.docx
Reach Out and Touch Someone: Haptics and Empathic Computing
Understanding_Digital_Forensics_Presentation.pptx
Unlocking AI with Model Context Protocol (MCP)

PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA

  • 1. Chapter - 5 Network – 5.2 Maulik Borsaniya Gardividyapith
  • 2. What is network ? • A computer network, or data network, is a digital telecommunications network which allows nodes to share resources. In computer networks, computing devices exchange data with each other using connections between nodes
  • 3. What is protocol ? • A network protocol defines rules and conventions for communication between network devices. • Network protocols include mechanisms for devices to identify and make connections with each other, as well as formatting rules that specify how data is packaged into messages sent and received • FTP: File Transfer Protocol. ... • SSL: Secure Sockets Layer. ... • TELNET (telnet) ... • SMTP: Simple Mail Transfer Protocol. ... • POP3: Post Office Protocol.
  • 4. Starting With the Socket Programming • Socket programming is a way of connecting two nodes on a network to communicate with each other. One socket(node) listens on a particular port at an IP, while other socket reaches out to the other to form a connection. • Server forms the listener socket while client reaches out to the server. • They are the real backbones behind web browsing. In simpler terms there is a server and a client. • Socket programming is started by importing the socket library and making a simple socket
  • 5. • import socket s =socket.socket(socket.AF_INET, socket.SOCK_STREAM) • Here we made a socket instance and passed it two parameters. • The first parameter is AF_INET and the • second one is SOCK_STREAM. • AF_INET refers to the address family ipv4. • The SOCK_STREAM means connection oriented TCP protocol.
  • 6. Here is an example of a script for connecting to Google An example script to connect to Google using socket # programming in Python import socket # for socket import sys try: s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print ("Socket successfully created") except socket.error as err: print ("socket creation failed with error") # default port for socket port = 80 try: host_ip = socket.gethostbyname('www.google.com') except socket.gaierror: # this means could not resolve the host print ("there was an error resolving the host") sys.exit() # connecting to the server s.connect((host_ip, port)) print ("the socket has successfully connected to google on port == %s" %(host_ip))
  • 7. Http Request Reponses • HTTP is based on the client-server architecture model and a stateless request/response protocol that operates by exchanging messages across a reliable TCP/IP connection. • An HTTP "client" is a program (Web browser or any other client) that establishes a connection to a server for the purpose of sending one or more HTTP request messages. An HTTP "server" is a program ( generally a web server like Apache Web Server or Internet Information Services IIS, etc. ) • that accepts connections in order to serve HTTP requests by sending HTTP response messages.
  • 8. Installation of http requests Navigate  Python34/script Pip install requests
  • 9. Download images from web – with request response import requests image_url = "https://www.python.org/static/community_logos/python-logo-master-v3- TM.png" # URL of the image to be downloaded is defined as image_url r = requests.get(image_url) # create HTTP response object # send a HTTP request to the server and save # the HTTP response in a response object called r with open("python_logo.png",'wb') as f: # Saving received content as a png file in # binary format # write the contents of the response (r.content) # to a new file in binary mode. f.write(r.content)
  • 10. Client Server Connection Establishment #Server # first of all import the socket library import socket # next create a socket object s = socket.socket() print ("Socket successfully created") # reserve a port on your computer in our # case it is 12345 but it can be anything port = 12345 # Next bind to the port # we have not typed any ip in the ip field # instead we have inputted an empty string # this makes the server listen to requests # coming from other computers on the network s.bind(('', port)) print ("socket binded to %s" %(port)) # put the socket into listening mode s.listen(5) print ("socket is listening") # a forever loop until we interrupt it or # an error occurs while True: # Establish connection with client. c, addr = s.accept() print ('Got connection from', addr) # send a thank you message to the client. c.send('Thank you for connecting') # Close the connection with the client Python server.py telnet localhost 12345
  • 11. A server has a bind() method which binds it to a specific ip and port so that it can listen to incoming requests on that ip and port. A server has a listen() method which puts the server into listen mode. This allows the server to listen to incoming connections. And last a server has an accept() and close() method. The accept method initiates a connection with the client and the close method closes the connection with the client.
  • 12. • First of all we import socket which is necessary. • Then we made a socket object and reserved a port on our pc. • After that we binded our server to the specified port. Passing an empty string means that the server can listen to incoming connections from other computers as well. If we would have passed 127.0.0.1 then it would have listened to only those calls made within the local computer. • After that we put the server into listen mode.5 here means that 5 connections are kept waiting if the server is busy and if a 6th socket try's to connect then the connection is refused. • At last we make a while loop and start to accept all incoming connections and close those connections after a thank you message to all connected sockets.
  • 13. #Client # Import socket module import socket # Create a socket object s = socket.socket() # Define the port on which you want to connect port = 12345 # connect to the server on local computer s.connect(('127.0.0.1', port)) # receive data from the server s.recv(1024) # close the connection s.close()
  • 14. TCP & UDP • The user datagram protocol (UDP) works differently from TCP/IP. • Where TCP is a stream oriented protocol, ensuring that all of the data is transmitted in the right order, • UDP is a message oriented protocol. • UDP does not require a long-lived connection, so setting up a UDP socket is a little simpler. • On the other hand, UDP messages must fit within a single packet (for IPv4, that means they can only hold 65,507 bytes because the 65,535 byte packet also includes header information) and delivery is not guaranteed as it is with TCP.
  • 15. Sending mail # Python code to illustrate Sending mail from #error # your Gmail account import smtplib # creates SMTP session s = smtplib.SMTP('smtp.gmail.com', 587) # start TLS for security s.starttls() # Authentication s.login("patelmaulik752@gmail.com", “password") # message to be sent message = "Test Message" # sending the mail s.sendmail("borsaniyamaulik@gmail.com", "inspirationalstory1@gmail.com", message) # terminating the session s.quit()
  • 16. success import smtplib gmailaddress = input("what is your gmail address? n ") gmailpassword = input("what is the password for that email address? n ") mailto = input("what email address do you want to send your message to? n ") msg = input("What is your message? n ") mailServer = smtplib.SMTP('smtp.gmail.com' , 587) mailServer.starttls() mailServer.login(gmailaddress , gmailpassword) mailServer.sendmail(gmailaddress, mailto , msg) print(" n Sent!") mailServer.quit()