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

PDF
What is Socket Programming in Python | Edureka
PDF
Socket programming using java
PPTX
Java socket programming
PPT
Java Socket Programming
PDF
Basic web architecture
PPTX
Java RMI
PPTX
Socket programming in python
PPTX
Java servlets
What is Socket Programming in Python | Edureka
Socket programming using java
Java socket programming
Java Socket Programming
Basic web architecture
Java RMI
Socket programming in python
Java servlets

What's hot (20)

PDF
Threads concept in java
PDF
Python3 (boto3) for aws
PPTX
Networking in Java
PPTX
Java Server Pages
PPTX
Socket programming in Java (PPTX)
PPT
Java Servlets
PPT
Oops concepts in php
PPT
Socket Programming Tutorial
PPTX
SQLite in Flutter.pptx
PPTX
Virtual memory managment
PPT
Basic socket programming
PPTX
Http protocol
PPTX
Access control list acl - permissions in linux
PDF
Introduction to OpenMP
PPT
Java Networking
PPT
Introduction to Android Fragments
PDF
Python network programming
PDF
Bootstrap 5 basic
PDF
Secure Session Management
PPT
Introduction to System Calls
Threads concept in java
Python3 (boto3) for aws
Networking in Java
Java Server Pages
Socket programming in Java (PPTX)
Java Servlets
Oops concepts in php
Socket Programming Tutorial
SQLite in Flutter.pptx
Virtual memory managment
Basic socket programming
Http protocol
Access control list acl - permissions in linux
Introduction to OpenMP
Java Networking
Introduction to Android Fragments
Python network programming
Bootstrap 5 basic
Secure Session Management
Introduction to System Calls
Ad

Similar to Socket programming (20)

PPTX
Raspberry pi Part 23
DOC
socket programming
DOC
socket programming
PPT
Md13 networking
PPT
Unit 8 Java
PPTX
PDF
NP-lab-manual.pdf
PDF
NP-lab-manual (1).pdf
DOCX
NP-lab-manual.docx
PPT
Network programming in Java
PPT
Network programming in Java
PPT
CHAT SERVER
PDF
CS6551 COMPUTER NETWORKS
PPT
Network Programming in Java
PDF
Netsim webinar-iitm-sep-17
PDF
Socket Programming
PPT
Computer Network in Network software.ppt
PPT
Socket Programming
PPT
Advances in computer networks, computer architecture
Raspberry pi Part 23
socket programming
socket programming
Md13 networking
Unit 8 Java
NP-lab-manual.pdf
NP-lab-manual (1).pdf
NP-lab-manual.docx
Network programming in Java
Network programming in Java
CHAT SERVER
CS6551 COMPUTER NETWORKS
Network Programming in Java
Netsim webinar-iitm-sep-17
Socket Programming
Computer Network in Network software.ppt
Socket Programming
Advances in computer networks, computer architecture
Ad

Recently uploaded (20)

PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
Construction Project Organization Group 2.pptx
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
Well-logging-methods_new................
PDF
composite construction of structures.pdf
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Digital Logic Computer Design lecture notes
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
web development for engineering and engineering
PPTX
Lecture Notes Electrical Wiring System Components
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Construction Project Organization Group 2.pptx
Automation-in-Manufacturing-Chapter-Introduction.pdf
Foundation to blockchain - A guide to Blockchain Tech
CYBER-CRIMES AND SECURITY A guide to understanding
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Well-logging-methods_new................
composite construction of structures.pdf
bas. eng. economics group 4 presentation 1.pptx
CH1 Production IntroductoryConcepts.pptx
Digital Logic Computer Design lecture notes
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Model Code of Practice - Construction Work - 21102022 .pdf
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx

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