Socket Programming in Python PDF
Socket Programming in Python PDF
Socket Programming in Python PDF
Socket Basics
A network socket is an endpoint of an inter-process communication flow across a computer
network. Sockets may communicate within a process, between processes on the same machine, or
between processes on different continents. Today, most communication between computers is
based on the internet protocol; therefore most network sockets are internet sockets. To create a
connection between machines, Python programs import the socket module, create a socket object,
and call the object’s methods to establish connections and send and receive data. Sockets are the
endpoints of a bidirectional communications channel.
Socket in Python
Python provides two levels of access to network services. At a low level, you can access the basic
socket support in the underlying operating system, which allows you to implement clients and
servers for both connection-oriented and connectionless protocols. Python also has libraries that
provide higher level access to specific application level network protocols, such as FTP, HTTP,
SMTP, and so on.
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.
Vocabulary of Sockets
Term Description
The family of protocols that will be used as the transport mechanism. These values are
domain
constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
The type of communications between the two endpoints, typically SOCK_STREAM for
type
connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
Typically zero, this may be used to identify a variant of a protocol within a domain
protocol
and type.
The identifier of a network interface:
• A string, which can be a host name, a dotted-quad address, or an IPV6 address in
colon (and possibly dot) notation
hostname
• A string "<broadcast>", which specifies an INADDR_BROADCAST address.
• A zero-length string, which specifies INADDR_ANY, or
• An Integer, interpreted as a binary address in host byte order.
Each server listens for clients calling on one or more ports. A port may be a Fixnum
port
port number, a string containing a port number, or the name of a service.
1
Socket Programming in Python
Once you have socket object, then you can use required functions to create your client or server
program.
A Simple Server
To write Internet servers, we use the socket function available in socket module to create a socket
object. A socket object is then used to call other functions to setup a socket server.
Now call bind(hostname, port) function to specify a port for your service on the given host.
Next, call the accept method of the returned object. This method waits until a client connects to the
port you specified, and then returns a connection object that represents the connection to that client.
2
Socket Programming in Python
#!C:\Python33\python.exe
# Echo server program
import socket
host = socket.gethostname()
port = 12345
s = socket.socket()
s.bind((host, port))
s.listen(5)
conn, addr = s.accept()
print('Got connection from ', addr[0], '(', addr[1], ')')
print('Thank you for connecting')
while True:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
A Simple Client
Now we will write a very simple client program which will open a connection to a given port
12345 and given host. This is very simple to create a socket client using Python's socket module
function.
The socket.connect(hosname, port ) opens a TCP connection to hostname on the port. Once you
have a socket open, you can read from it like any IO object. When done, remember to close it, as
you would close a file.
The following code is a very simple client that connects to a given host and port, reads any
available data from the socket, and then exits:
#!C:\Python33\python.exe
# Echo client program
import socket
host = socket.gethostname()
port = 12345
s = socket.socket()
s.connect((host, port))
s.sendall(b'Welcome User!')
data = s.recv(1024)
s.close()
print(repr(data))
Now run this server.py in background and then run above client.py to see the result.
Output:
Step 1: Run server.py. It would start a server in background.
Step 2: Run client.py. Once server is started run client.
Step 3: Output of server.py generates as follows:
3
Socket Programming in Python