Python Socket Programming
Python Socket Programming
application application
socket controlled by
process process app developer
transport transport
network network controlled
link
by OS
link Internet
physical physical
App
3 2 App D1
1
socket Dest.
3 2
1
socket D2
D3
Client/Server Concepts
Server opens a specific port
The one associated with its service
Then just waits for requests
Server is the passive opener
Clients get ephemeral ports
Guaranteed unique, 1024 or greater
Uses them to communicate with server
Client is the active opener
socket() socket()
bind() bind()
recvfrom() sendto()
[blocked] recvfrom()
[blocked]
sendto()
SERVER CLIENT
Python Basics 2-9
Simple Connectionless Server
from socket import socket,
AF_INET, SOCK_DGRAM
s = socket(AF_INET, SOCK_DGRAM)
s.bind(('127.0.0.1', 11111))
while True:
data, addr = s.recvfrom(1024)
print "Connection from", addr
s.sendto(data.upper(), addr)
listen()
connect()
accept()
write()
[blocked]
read()
read() [blocked]
[blocked]
write()
When interaction is over, server
loops to accept a new connection
Application Example:
1. Client reads a line of characters (data) from its
keyboard and sends the data to the server.
2. The server receives the data and converts
characters to uppercase.
3. The server sends the modified data to the client.
4. The client receives the modified data and displays
the line on its screen.
Python Basics 2-17
Socket programming with UDP
UDP: no “connection” between client & server
no handshaking before sending data
sender explicitly attaches IP destination address and
port # to each packet
rcvr extracts sender IP address and port# from
received packet
UDP: transmitted data may be lost or received
out-of-order
Application viewpoint:
UDP provides unreliable transfer of groups of bytes
(“datagrams”) between client and server
write reply to
serverSocket read datagram from
specifying clientSocket
client address,
port number close
clientSocket
Python Basics 24
Binding and Accepting
This is only relevant if your socket is intended to
be a server.
Binding
If you are planning to accept incoming
connections, you must bind your socket.
Whereas connecting forms a connection with a
remote socket,
binding tells your socket that it should use a
specific port when looking for incoming
connections.
Python Basics 25
Binding and Accepting…
You bind a socket using the bind() function,
which takes 2 arguments:
the hostname that you wish to bind to, and the
port you wish to bind to.
In general, the hostname will be your hostname,
so you can use the gethostbyname() function
from the socket module as the hostname
argument.
Once it is bound to your hostname and to a
specific port, the socket knows that when told to
listen, it should listen at that port.
Python Basics 26
Binding and Accepting…
import socket
server = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server.bind(socket.gethostbyname(), 123) or
server.bind(“”, 31337) or
Listening
Once the socket is bound to a port, you must tell
it to listen at that port.
Listening; refers to monitoring a port so that it
can handle anything that tries to connect to that
port.
Python Basics 27
Binding and Accepting…
The listen() function does this, listening to
connection attempts.
It takes one argument, an integer value
representing the maximum number of queued
connection attempts to hold before dropping
older ones.
import socket
server = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server.bind(“”, 123)
server.listen(1) #listen at port 123 and queue only 1
connection at a time
After a connection is made, it must be accepted.
Python Basics 28
Binding and Accepting…
Accepting
listen() finds connection attempts, but you must
use then accept them to form a connection
between your host and the remote client.
You use the aptly-named accept() function in
order to do this.
import socket
server = socket.socket(socket.AF_INET,
socket.SOCK_STREAM)
server.bind(“”,123)
server.listen(1)
connection, sock_addr = sock.accept()
Python Basics 29
Binding and Accepting…
As you can see, the accept() function creates an
entirely new socket
your original socket (in this case 'sock') can
continue listening for new connections
while the newly created socket (in this case
'connection') can be used to send and receive data
from the client. Passive Participant
a-sock-1 l-sock a-sock-2
socket socket
Active 1 Active 2
Python Basics 30
Sending and Receiving
Whether you have connected to a remote host or
accepted a connection from a client, sending and
receiving are what allow data to be transferred.
The send() and recv() functions from the socket
module are basically identical - the only
difference is whether data is being sent or
received.
They both take one argument
send() takes the data to be sent as an argument,
recv() takes the number of bytes to be received
as an argument. recv() returns the data received.
Python Basics 31
Sending and Receiving…
sock = socket.socket(socket.AF_INET,
socket.SOCK_STREAM
sock.connect(("iamu.edu.et", 123
nickname = "NICK mamush”
encoded_nick = bytes(nickname, 'utf-8')
sock.send(encoded_nick)
sock.send(encoded_user)
Python Basics 32
Client/server socket interaction: TCP
server (running on hostid) client
create socket,
port=x, for incoming
request:
serverSocket = socket()
write reply to
connectionSocket read reply from
clientSocket
close
connectionSocket close
clientSocket
OS
packet Transport (TCP/UDP)