SlideShare a Scribd company logo
Socket Programming in Python
Nemi Chandra Rathore
Department of Computer Science
Central University of South Bihar, Patna
August 25, 2017
Outline
Introduction
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 2 / 19
Introduction to Sockets
A socket is one endpoint of a two-way communication link between
two programs running on the network.[1]
process sends/receives messages to/from its socket.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 3 / 19
Introduction to Sockets
Figure: Process communication on network using sockets(TCP)
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 4 / 19
Introduction to Sockets
A socket is bound to a port number so that the TCP layer can
identify the application that data is destined to be sent to.
On the Server Side:
Normally, a server runs on a specific computer and has a socket that is
bound to a specific port number.
The server just waits, listening to the socket for a client to make a
connection request.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 5 / 19
Introduction to Sockets
On the Client-Side:
The client knows the hostname of the machine on which the server is
running and the port number on which the server is listening.
To make a connection request, the client connect with the server on
the server’s machine and port.
The client also needs to identify itself to the server so it binds to a
local port number that it will use during this connection.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 6 / 19
Socket Programming in Python
Python provides two levels of access to network services [2]:
1 Low Level Access
Offers access the basic socket support in the underlying operating
system.
2 High Level Access
Offers libraries that provide higher-level access to specific
application-level network protocols, such as FTP, HTTP, and so on.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 7 / 19
Socket Programming in Python
Sockets may be implemented over a number of different channel
types: Unix domain sockets, TCP, UDP, and so on.
The socket library provides specific classes for handling the common
transports as well as a generic interface for handling the rest.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 8 / 19
Socket Programming in Python
Term Decscription
domain The family of protocols that is used as the trans-
port mechanism. Indicated by constants like
AF INET, AF INET6, PF UNIX, PF X25 and so
on.
type The type of communications between the two end-
points, SOCK STREAM, SOCK DGRAM
protocol Used to identify a variant of a protocol, typically
0.
hostname A string representing host machine.(IP Address)
port A string containing a port number, or the name of
a service.
Table: Socket vocabulary
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 9 / 19
Socket Programming in Python: Creating a server
Import socket API using ”import socket” statement.
Create a socket, you must use the socket.socket() function available
in socket module
s = socket.socket(socket family, socket type, protocol = 0)
Example:
s = socket.socket(socket.AF INET, socket.SOCK STREAM)
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 10 / 19
Socket Programming in Python: Creating a server
Now bind the socket using bind function with a port and IP.
s.bind(IP, PORT)
This makes the server listen to requests coming from other computers
on the network on PORT
Now put the socket into listening mode by adding line s.listen(51).
1
5 indicates that 5 connections are kept waiting if the server is busy.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 11 / 19
Socket Programming in Python: Creating a server
Now create a forever loop until we interrupt it or an error occurs:
accept and process any request from client within the loop.
Use s.accept() to accept a connection from client.
s.accept returns a tuple conn, addr where conn is a connection object
to the client and addr is address of the client.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 12 / 19
Socket Programming in Python: Creating a client
Import socket API using ”import socket” statement.
Create a socket, you must use the socket.socket() function available
in socket module.
Connect the set server running at machine with (IP, port) pair using
s.connect((IP, port))
Now client can communicate using send() and recv() methods of the
socket object.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 13 / 19
Socket Programming in Python
Table: Server Socket Methods
Method Decscription
s.bind() This method binds address (hostname, port num-
ber pair) to socket.
s.listen() This method sets up and start TCP listener.
s.accept() This passively accept TCP client connection, wait-
ing until connection arrives (blocking).
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 14 / 19
Socket Programming in Python
Table: Cleint Socket Methods
Method Decscription
s.connect() This method actively initiates TCP server connec-
tion.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 15 / 19
Socket Programming in Python
Table: General Socket Methods
Method Decscription
s.recv() Receives TCP message.
s.send() Transmits TCP message.
s.recvfrom() Receives UDP message.
s.sendto() Transmits UDP message.
s.close() Closes socket.
socket.gethostname() Returns the hostname.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 16 / 19
Socket Programming in Python
Protocol Common func-
tion
Port
No
Python module
HTTP Web pages 80 httplib, urllib, xml-
rpclib
NNTP2 Usenet news 119 nntplib
FTP File transfers 20 ftplib, urllib
SMTP Sending email 25 smtplib
Telnet Command lines 23 telnetlib
IMAP4 Fetching email 143 imaplib
POP3 Fetching email 110 poplib
Table: Python Internet modules
2
Network News Transfer Protocol
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 17 / 19
Socket Programming in Python
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 18 / 19
References I
Oracle: Java Documentation. https://docs.oracle.com/
javase/tutorial/networking/sockets/definition.html.
Accessed: 2017-08-20.
Python Network Programming. https:
//www.tutorialspoint.com/python/python_networking.htm.
Accessed: 2017-08-20.
Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 19 / 19

More Related Content

What's hot

Slides for protocol layering and network applications
Slides for protocol layering and network applicationsSlides for protocol layering and network applications
Slides for protocol layering and network applications
jajinekkanti
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
Ajit Nayak
 
Socket programming
Socket programmingSocket programming
Socket programming
harsh_bca06
 
Distributed system architecture
Distributed system architectureDistributed system architecture
Distributed system architecture
Yisal Khan
 
Intro (Distributed computing)
Intro (Distributed computing)Intro (Distributed computing)
Intro (Distributed computing)Sri Prasanna
 
ICMP
ICMPICMP
TCP/IP Protocol Architeture
TCP/IP Protocol ArchitetureTCP/IP Protocol Architeture
TCP/IP Protocol Architeture
Manoj Kumar
 
remote procedure calls
  remote procedure calls  remote procedure calls
remote procedure callsAshish Kumar
 
GO BACK N PROTOCOL
GO BACK N PROTOCOLGO BACK N PROTOCOL
GO BACK N PROTOCOL
shayan singla
 
Socket programming
Socket programmingSocket programming
Socket programming
Anurag Tomar
 
Working with NS2
Working with NS2Working with NS2
Working with NS2
chanchal214
 
Remote procedure call on client server computing
Remote procedure call on client server computingRemote procedure call on client server computing
Remote procedure call on client server computing
Satya P. Joshi
 
02 protocols and tcp-ip
02 protocols and tcp-ip02 protocols and tcp-ip
02 protocols and tcp-ip
kashish0313
 
OSI Physical Layer
OSI Physical LayerOSI Physical Layer
OSI Physical Layer
Sachii Dosti
 
Chapter 4 slides
Chapter 4 slidesChapter 4 slides
Chapter 4 slideslara_ays
 
19 Network Layer Protocols
19 Network Layer Protocols19 Network Layer Protocols
19 Network Layer Protocols
Meenakshi Paul
 

What's hot (20)

Slides for protocol layering and network applications
Slides for protocol layering and network applicationsSlides for protocol layering and network applications
Slides for protocol layering and network applications
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Distributed system architecture
Distributed system architectureDistributed system architecture
Distributed system architecture
 
Intro (Distributed computing)
Intro (Distributed computing)Intro (Distributed computing)
Intro (Distributed computing)
 
ICMP
ICMPICMP
ICMP
 
Sockets
SocketsSockets
Sockets
 
TCP/IP Protocol Architeture
TCP/IP Protocol ArchitetureTCP/IP Protocol Architeture
TCP/IP Protocol Architeture
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
remote procedure calls
  remote procedure calls  remote procedure calls
remote procedure calls
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
GO BACK N PROTOCOL
GO BACK N PROTOCOLGO BACK N PROTOCOL
GO BACK N PROTOCOL
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Working with NS2
Working with NS2Working with NS2
Working with NS2
 
Remote procedure call on client server computing
Remote procedure call on client server computingRemote procedure call on client server computing
Remote procedure call on client server computing
 
02 protocols and tcp-ip
02 protocols and tcp-ip02 protocols and tcp-ip
02 protocols and tcp-ip
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
OSI Physical Layer
OSI Physical LayerOSI Physical Layer
OSI Physical Layer
 
Chapter 4 slides
Chapter 4 slidesChapter 4 slides
Chapter 4 slides
 
19 Network Layer Protocols
19 Network Layer Protocols19 Network Layer Protocols
19 Network Layer Protocols
 

Similar to Socket programming

Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla
 
socket programming
 socket programming  socket programming
socket programming
prashantzagade
 
socket programming
socket programming socket programming
socket programming
prashantzagade
 
Java 1
Java 1Java 1
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
RaviRajput416403
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
RaviRajput416403
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
RaviRajput416403
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
Tushar B Kute
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
Tushar B Kute
 
CHAT SERVER
CHAT SERVERCHAT SERVER
CHAT SERVER
Sine19
 
CS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKSCS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKS
Kathirvel Ayyaswamy
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
Tushar B Kute
 
Netsim webinar-iitm-sep-17
Netsim webinar-iitm-sep-17Netsim webinar-iitm-sep-17
Netsim webinar-iitm-sep-17
SANJAY ANAND
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran
 
Advances in computer networks, computer architecture
Advances in computer networks, computer architectureAdvances in computer networks, computer architecture
Advances in computer networks, computer architecture
sandhyagowdah
 

Similar to Socket programming (20)

Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Python networking
Python networkingPython networking
Python networking
 
socket programming
 socket programming  socket programming
socket programming
 
socket programming
socket programming socket programming
socket programming
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Java 1
Java 1Java 1
Java 1
 
NP-lab-manual (1).pdf
NP-lab-manual (1).pdfNP-lab-manual (1).pdf
NP-lab-manual (1).pdf
 
NP-lab-manual.pdf
NP-lab-manual.pdfNP-lab-manual.pdf
NP-lab-manual.pdf
 
NP-lab-manual.docx
NP-lab-manual.docxNP-lab-manual.docx
NP-lab-manual.docx
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
Networking in Java
Networking in JavaNetworking in Java
Networking in Java
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
CHAT SERVER
CHAT SERVERCHAT SERVER
CHAT SERVER
 
CS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKSCS6551 COMPUTER NETWORKS
CS6551 COMPUTER NETWORKS
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
Netsim webinar-iitm-sep-17
Netsim webinar-iitm-sep-17Netsim webinar-iitm-sep-17
Netsim webinar-iitm-sep-17
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Advances in computer networks, computer architecture
Advances in computer networks, computer architectureAdvances in computer networks, computer architecture
Advances in computer networks, computer architecture
 
Linux Network Management
Linux Network ManagementLinux Network Management
Linux Network Management
 

Recently uploaded

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
Robbie Edward Sayers
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
Divya Somashekar
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
Amil Baba Dawood bangali
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
ViniHema
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
Kamal Acharya
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
Kamal Acharya
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
ShahidSultan24
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
PrashantGoswami42
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
abh.arya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
Kamal Acharya
 

Recently uploaded (20)

Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
HYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generationHYDROPOWER - Hydroelectric power generation
HYDROPOWER - Hydroelectric power generation
 
block diagram and signal flow graph representation
block diagram and signal flow graph representationblock diagram and signal flow graph representation
block diagram and signal flow graph representation
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
NO1 Uk best vashikaran specialist in delhi vashikaran baba near me online vas...
 
power quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptxpower quality voltage fluctuation UNIT - I.pptx
power quality voltage fluctuation UNIT - I.pptx
 
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdfCOLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
COLLEGE BUS MANAGEMENT SYSTEM PROJECT REPORT.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
Final project report on grocery store management system..pdf
Final project report on grocery store management system..pdfFinal project report on grocery store management system..pdf
Final project report on grocery store management system..pdf
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
addressing modes in computer architecture
addressing modes  in computer architectureaddressing modes  in computer architecture
addressing modes in computer architecture
 
Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.Quality defects in TMT Bars, Possible causes and Potential Solutions.
Quality defects in TMT Bars, Possible causes and Potential Solutions.
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Democratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek AryaDemocratizing Fuzzing at Scale by Abhishek Arya
Democratizing Fuzzing at Scale by Abhishek Arya
 
Courier management system project report.pdf
Courier management system project report.pdfCourier management system project report.pdf
Courier management system project report.pdf
 

Socket programming

  • 1. Socket Programming in Python Nemi Chandra Rathore Department of Computer Science Central University of South Bihar, Patna August 25, 2017
  • 2. Outline Introduction Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 2 / 19
  • 3. Introduction to Sockets A socket is one endpoint of a two-way communication link between two programs running on the network.[1] process sends/receives messages to/from its socket. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 3 / 19
  • 4. Introduction to Sockets Figure: Process communication on network using sockets(TCP) Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 4 / 19
  • 5. Introduction to Sockets A socket is bound to a port number so that the TCP layer can identify the application that data is destined to be sent to. On the Server Side: Normally, a server runs on a specific computer and has a socket that is bound to a specific port number. The server just waits, listening to the socket for a client to make a connection request. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 5 / 19
  • 6. Introduction to Sockets On the Client-Side: The client knows the hostname of the machine on which the server is running and the port number on which the server is listening. To make a connection request, the client connect with the server on the server’s machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 6 / 19
  • 7. Socket Programming in Python Python provides two levels of access to network services [2]: 1 Low Level Access Offers access the basic socket support in the underlying operating system. 2 High Level Access Offers libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 7 / 19
  • 8. Socket Programming in Python Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 8 / 19
  • 9. Socket Programming in Python Term Decscription domain The family of protocols that is used as the trans- port mechanism. Indicated by constants like AF INET, AF INET6, PF UNIX, PF X25 and so on. type The type of communications between the two end- points, SOCK STREAM, SOCK DGRAM protocol Used to identify a variant of a protocol, typically 0. hostname A string representing host machine.(IP Address) port A string containing a port number, or the name of a service. Table: Socket vocabulary Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 9 / 19
  • 10. Socket Programming in Python: Creating a server Import socket API using ”import socket” statement. Create a socket, you must use the socket.socket() function available in socket module s = socket.socket(socket family, socket type, protocol = 0) Example: s = socket.socket(socket.AF INET, socket.SOCK STREAM) Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 10 / 19
  • 11. Socket Programming in Python: Creating a server Now bind the socket using bind function with a port and IP. s.bind(IP, PORT) This makes the server listen to requests coming from other computers on the network on PORT Now put the socket into listening mode by adding line s.listen(51). 1 5 indicates that 5 connections are kept waiting if the server is busy. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 11 / 19
  • 12. Socket Programming in Python: Creating a server Now create a forever loop until we interrupt it or an error occurs: accept and process any request from client within the loop. Use s.accept() to accept a connection from client. s.accept returns a tuple conn, addr where conn is a connection object to the client and addr is address of the client. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 12 / 19
  • 13. Socket Programming in Python: Creating a client Import socket API using ”import socket” statement. Create a socket, you must use the socket.socket() function available in socket module. Connect the set server running at machine with (IP, port) pair using s.connect((IP, port)) Now client can communicate using send() and recv() methods of the socket object. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 13 / 19
  • 14. Socket Programming in Python Table: Server Socket Methods Method Decscription s.bind() This method binds address (hostname, port num- ber pair) to socket. s.listen() This method sets up and start TCP listener. s.accept() This passively accept TCP client connection, wait- ing until connection arrives (blocking). Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 14 / 19
  • 15. Socket Programming in Python Table: Cleint Socket Methods Method Decscription s.connect() This method actively initiates TCP server connec- tion. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 15 / 19
  • 16. Socket Programming in Python Table: General Socket Methods Method Decscription s.recv() Receives TCP message. s.send() Transmits TCP message. s.recvfrom() Receives UDP message. s.sendto() Transmits UDP message. s.close() Closes socket. socket.gethostname() Returns the hostname. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 16 / 19
  • 17. Socket Programming in Python Protocol Common func- tion Port No Python module HTTP Web pages 80 httplib, urllib, xml- rpclib NNTP2 Usenet news 119 nntplib FTP File transfers 20 ftplib, urllib SMTP Sending email 25 smtplib Telnet Command lines 23 telnetlib IMAP4 Fetching email 143 imaplib POP3 Fetching email 110 poplib Table: Python Internet modules 2 Network News Transfer Protocol Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 17 / 19
  • 18. Socket Programming in Python Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 18 / 19
  • 19. References I Oracle: Java Documentation. https://docs.oracle.com/ javase/tutorial/networking/sockets/definition.html. Accessed: 2017-08-20. Python Network Programming. https: //www.tutorialspoint.com/python/python_networking.htm. Accessed: 2017-08-20. Nemi Chandra Rathore (Department of Computer Science Central University of South Bihar, Patna)Socket Programming in Python August 25, 2017 19 / 19