Python Networking PDF
Python Networking PDF
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, and so on.
This chapter gives you understanding on most famous concept in Networking - Socket
Programming.
What is Sockets?
Sockets are the endpoints of a bidirectional communications channel. Sockets may communicate
within a process, between processes on the same machine, or between processes on different
continents.
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.
Term Description
domain The family of protocols that is used as the transport mechanism. These
values are constants such as AF_INET, PF_INET, PF_UNIX, PF_X25, and so on.
protocol Typically zero, this may be used to identify a variant of a protocol within a
domain and type.
port Each server listens for clients calling on one or more ports. A port may be a
Fixnum port number, a string containing a port number, or the name of a
service.
Once you have socket object, then you can use required functions to create your client or server
program. Following is the list of functions required −
Method Description
s.accept This passively accept TCP client connection, waiting until connection arrives
blocking.
Method Description
Method Description
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 bindhostname, 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.
A Simple Client
Let us write a very simple client program which opens 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.connecthosname, 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 −
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
Now run this server.py in background and then run above client.py to see the result.
$ python client.py
Please check all the libraries mentioned above to work with FTP, SMTP, POP, and IMAP protocols.
Further Readings
This was a quick start with Socket Programming. It is a vast subject. It is recommended to go
through the following link to find more detail −