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

Socket programming

  • 1.
    Socket Programming inPython 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 Asocket 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 Asocket 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 Onthe 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 inPython 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 inPython 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 inPython 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 inPython: 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 inPython: 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 inPython: 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 inPython: 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 inPython 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 inPython 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 inPython 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 inPython 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 inPython 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: JavaDocumentation. 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