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

What's hot

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programmingelliando dias
Ā 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
Ā 
A Short Java Socket Tutorial
A Short Java Socket TutorialA Short Java Socket Tutorial
A Short Java Socket TutorialGuo Albert
Ā 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket ProgrammingMousmi Pawar
Ā 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programmingKristian Arjianto
Ā 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
Ā 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
Karim Sonbol
Ā 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
Saksham Khurana
Ā 
Networking in java
Networking in javaNetworking in java
Networking in java
shravan kumar upadhayay
Ā 
Python Sockets
Python SocketsPython Sockets
Python Sockets
pythontic
Ā 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
rajshreemuthiah
Ā 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
babak danyal
Ā 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
kamal kotecha
Ā 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
Shree M.L.Kakadiya MCA mahila college, Amreli
Ā 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
Sayak Sarkar
Ā 
Socket programming
Socket programmingSocket programming
Socket programming
chandramouligunnemeda
Ā 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-python
Yuvaraja Ravi
Ā 
java networking
 java networking java networking
java networking
Waheed Warraich
Ā 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
ashok hirpara
Ā 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket based
Mukesh Tekwani
Ā 

What's hot (20)

Application Layer and Socket Programming
Application Layer and Socket ProgrammingApplication Layer and Socket Programming
Application Layer and Socket Programming
Ā 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Ā 
A Short Java Socket Tutorial
A Short Java Socket TutorialA Short Java Socket Tutorial
A Short Java Socket Tutorial
Ā 
Networking Java Socket Programming
Networking Java Socket ProgrammingNetworking Java Socket Programming
Networking Java Socket Programming
Ā 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
Ā 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
Ā 
Network programming Using Python
Network programming Using PythonNetwork programming Using Python
Network programming Using Python
Ā 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
Ā 
Networking in java
Networking in javaNetworking in java
Networking in java
Ā 
Python Sockets
Python SocketsPython Sockets
Python Sockets
Ā 
Tcp/ip server sockets
Tcp/ip server socketsTcp/ip server sockets
Tcp/ip server sockets
Ā 
Tcp sockets
Tcp socketsTcp sockets
Tcp sockets
Ā 
Network programming in java - PPT
Network programming in java - PPTNetwork programming in java - PPT
Network programming in java - PPT
Ā 
Networking in python by Rj
Networking in python by RjNetworking in python by Rj
Networking in python by Rj
Ā 
Java API: java.net.InetAddress
Java API: java.net.InetAddressJava API: java.net.InetAddress
Java API: java.net.InetAddress
Ā 
Socket programming
Socket programmingSocket programming
Socket programming
Ā 
Socket programming-in-python
Socket programming-in-pythonSocket programming-in-python
Socket programming-in-python
Ā 
java networking
 java networking java networking
java networking
Ā 
Advance Java-Network Programming
Advance Java-Network ProgrammingAdvance Java-Network Programming
Advance Java-Network Programming
Ā 
Java networking programs socket based
Java networking programs socket basedJava networking programs socket based
Java networking programs socket based
Ā 

Similar to PYTHON -Chapter 5 NETWORK - MAULIK BORSANIYA

python programming
python programmingpython programming
python programming
keerthikaA8
Ā 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Javaarnold 7490
Ā 
Socket programming
Socket programmingSocket programming
Socket programming
Anurag Tomar
Ā 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
Ā 
Networking.pptx
Networking.pptxNetworking.pptx
Networking.pptx
Esubesisay
Ā 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
Ghadeer AlHasan
Ā 
Sockets
SocketsSockets
Socketssivindia
Ā 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
EliasPetros
Ā 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
Ā 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Nitish Nagar
Ā 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Wipro
Ā 
A.java
A.javaA.java
A.java
JahnaviBhagat
Ā 
Java 1
Java 1Java 1
Java 1
VidyaVarshini3
Ā 
Networking
NetworkingNetworking
Networkingnik.manjit
Ā 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Mukesh Tekwani
Ā 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project ReportKavita Sharma
Ā 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Samsil Arefin
Ā 
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriyaIPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
VijiPriya Jeyamani
Ā 
10 Networking
10 Networking10 Networking
10 Networking
Deepak Hagadur Bheemaraju
Ā 

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

python programming
python programmingpython programming
python programming
Ā 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
Ā 
Socket programming
Socket programmingSocket programming
Socket programming
Ā 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Ā 
Networking.pptx
Networking.pptxNetworking.pptx
Networking.pptx
Ā 
#1 (TCPvs. UDP)
#1 (TCPvs. UDP)#1 (TCPvs. UDP)
#1 (TCPvs. UDP)
Ā 
Sockets
SocketsSockets
Sockets
Ā 
5_6278455688045789623.pptx
5_6278455688045789623.pptx5_6278455688045789623.pptx
5_6278455688045789623.pptx
Ā 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Ā 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
Ā 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
Ā 
Lan chat system
Lan chat systemLan chat system
Lan chat system
Ā 
A.java
A.javaA.java
A.java
Ā 
Java 1
Java 1Java 1
Java 1
Ā 
Networking
NetworkingNetworking
Networking
Ā 
Java networking programs - theory
Java networking programs - theoryJava networking programs - theory
Java networking programs - theory
Ā 
Mail Server Project Report
Mail Server Project ReportMail Server Project Report
Mail Server Project Report
Ā 
Client server chat application
Client server chat applicationClient server chat application
Client server chat application
Ā 
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriyaIPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
IPT Chapter 2 Web Services and Middleware - Dr. J. VijiPriya
Ā 
10 Networking
10 Networking10 Networking
10 Networking
Ā 

More from Maulik Borsaniya

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

More from Maulik Borsaniya (7)

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

Recently uploaded

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
Ā 
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
UiPathCommunity
Ā 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
Ā 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
Ā 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
Ā 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
Ā 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
Ā 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
Ā 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
Ā 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
KatiaHIMEUR1
Ā 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
Ā 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
Ā 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
Ā 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
Ā 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
Ā 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
Ā 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
Ā 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
Ā 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
Ā 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
Ā 

Recently uploaded (20)

Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Ā 
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder ā€“ active learning and UiPath LLMs for do...
Ā 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
Ā 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Ā 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Ā 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Ā 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Ā 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
Ā 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Ā 
Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !Securing your Kubernetes cluster_ a step-by-step guide to success !
Securing your Kubernetes cluster_ a step-by-step guide to success !
Ā 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Ā 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
Ā 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Ā 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
Ā 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Ā 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
Ā 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Ā 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Ā 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Ā 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Ā 

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()