Professional Documents
Culture Documents
DP 13
DP 13
• Connectionless Sockets
• Socket process for connectionless clients
and servers
• socket() Module Function
• Socket Object (Built-In) Methods
• The SocketServer Module
Connectionless Sockets
2
13
Connectionless Sockets
3
13
Socket process for connectionless
clients and servers
4
13
Socket() Module Function
5
13
Socket() Module Function
udpSock = socket.socket
(socket.AF_INET, socket.SOCK_DGRAM)
6
13
Socket Object (Built-In) Methods
7
13
Socket Object (Built-In) Methods
s.setblocking()
s.settimeout()
s.gettimeout()
9
13
Socket Object (Built-In) Methods
Data Attributes
s.family
s.type
s.proto
The socket protocol
10
13
The Socket Server Module
11
13
The Socket Server Module
ForkingMixIn /
ThreadingMixIn
ThreadingTCPServer/
ThreadingUDPServer
12
13
The Socket Server Module
BaseRequestHandler
StreamRequestHandler /
DatagramRequestHandler
13
13
User Datagram Protocol
02 (UDP)
• User Datagram Protocol (UDP)
• Differences between the TCP and UDP
• Python Internet modules
• Implementing the Client
• Implementing the Server
User Datagram Protocol (UDP)
TCP UDP
Service Service
httplib, urllib,
HTTP Web pages 80
xmlrpclib
4
10
Python Internet modules
5
10
Implementing the Client
Import the socket python module, this can be done like so:
import socket
UDP_IP_ADDRESS = "127.0.0.1”
UDP_PORT_NO = 6789
clientSock.sendto(Message,(UDP_IP_ADDRESS,
UDP_PORT_NO))
7
10
Implementing the Server
import socket
UDP_IP_ADDRESS = "127.0.0.1"
UDP_PORT_NO = 6789
8
10
Implementing the Server
serverSock = socket.socket(socket.AF_INET,
socket.SOCK_DGRAM)
serverSock.bind((UDP_IP_ADDRESS,
UDP_PORT_NO))
9
10
Implementing the Server
while True:
data, addr = serverSock.recvfrom(1024)
print ("Message: ", data)
10
10
Building a Python
03 Networking Program