Python Network Programming


Python provides two levels of access to network services. At a low level, we can access the basic socket support in the underlying operating system, which would allows to implement clients and servers for both connection-oriented and connectionless protocols.

Python also has libraries that would provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.

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 would be implemented over a number of different channel types such as 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.

Socket Module

To create a socket, we must use the socket.socket() function available in socket module, which has the general syntax that are given as follows:

s = socket.socket (socket_family, socket_type, protocol=0)

The parameters can be described as follows

socket_family – This is either AF_UNIX or AF_INET.

socket_type – This is either SOCK_STREAM or SOCK_DGRAM.

protocol – This is usually left out, defaulting to 0.

Server Socket Methods

s.bind() – This method is used to binds the address (hostname, port number pair) to socket.
s.listen() – This method is used to sets up and start TCP listener.

s.accept() – This method passively accept TCP client connection, waiting until connection arrives (blocking).

Client Socket Methods

s.connect()This method actively initiates TCP server connection.

General Socket Methods

s.recv() – This method receives TCP message

s.send() – This method transmits TCP message

s.recvfrom() – This method receives UDP message

s.sendto() – This method transmits UDP message

s.close() – This method closes socket

socket.gethostname() – This method would returns the hostname.

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 used to call other functions to setup a socket server.

Call the bind(hostname, port) function to specify a port for the service on the given host.

Next, call the accept method of the returned object. This method waits until a client connects to the port specified, and then returns a connection object that represents the connection to that client.

A Simple Client

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.connect(hostname, port) opens a TCP connection to hostname on the port. Once the socket is open, read from it like any IO object. When done, remember to close it, as to close a file.





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s