Network Programing in Python

Table of Contents
Socket Introduction:...................................................................................................................................2
Sockets.......................................................................................................................................................2
Ports...........................................................................................................................................................2
Types of Internet Socket:.......................................................................................................................3
Stream Sockets (SOCK_STREAM) TCP........................................................................................3
Datagram Sockets (SOCK_DGRAM) UDP....................................................................................3
Raw sockets (or Raw IP sockets).....................................................................................................4
Why Python for Network Programing:......................................................................................................4
Socket Call.................................................................................................................................................4
TCP Socket Call:...................................................................................................................................4
UDP Socket Call:..................................................................................................................................5
Python socket Module:..........................................................................................................................5
Server Socket Methods:....................................................................................................................6
client socket methods:.......................................................................................................................6
General Socket methods:..................................................................................................................6
Simple TCP Server and Client:.............................................................................................................6
Socket API For Server Side:..................................................................................................................7
Socket API for client side:.....................................................................................................................8
The Socket API For Established Connections ......................................................................................8
TCP Echo Server and Client:...............................................................................................................9
UDP echo server and Client:...............................................................................................................10
Catching Exceptions ................................................................................................................................11
(Simple command Peer to Peer Chat application):.............................................................................12
Multitasking: (Handling Multiple clients)...............................................................................................14
Forking:....................................................................................................................................................14
A basic fork in action:.........................................................................................................................14
Zombie Processes:...............................................................................................................................15
Managing Zombie processes:..............................................................................................................16
Echo Server:........................................................................................................................................16
Echo Client:.........................................................................................................................................17
Threading:................................................................................................................................................18
Process VS Thread:.............................................................................................................................20
Creating threads in Python..................................................................................................................21
Examples:............................................................................................................................................22
Three parts of a multi-threaded server................................................................................................22
Echo server using Thread:...................................................................................................................23
Echo Client..........................................................................................................................................24
Asynchronous Communication:...............................................................................................................25
Operations of sockets:.........................................................................................................................25
Blocking Socket: (Synchronous )...................................................................................................25