Python - Network Programming
In this chapter of about Python - Network Programming.
Python have two levels of access to network services.
Python also has libraries that provide higher-level access to specific application-level network protocols, such as FTP, HTTP, and so on.
Sockets is communicate within a process, between processes on the same machine, or between processes on different continents.
A socket is a link between two applications that can communicate with one another (either locally on a single machine or remotely between two machines in separate locations).
Sockets may be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and so on
|Sr.No.||Term & Description|
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.
The type of communications between the two endpoints, mostly SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols.
It is used to identify a variant of a protocol within a domain and type.
The identifier of a network interface −
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.
The socket Module
In order to create a socket, you use the socket.socket() function, and the syntax is as simple as:
socket.socket (socket_family, socket_type, protocol
Here is the description of the arguments:
- socket_family: Represents the address (and protocol) family. It can be either AF_UNIX or AF_INET.
- socket_type: Represents the socket type, and can be either SOCK_STREAM or SOCK_DGRAM.
- protocol: This is an optional argument, and it usually defaults to 0.
Client Socket Methods
|Sr.No.||Method & Description|
This method actively initiates TCP server connection.
Build a Simple ServerLet's we can look at a simple Python server. The following are the socket server methods available in Python. A socket object is then used to call other functions to setup a socket server. There are following
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(): Binds address (hostname, port number) to socket.
s.listen(): Sets up and starts TCP listener.
s.accept(): Accepts TCP client connection.
import socket import sys # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Define host host = 'localhost' # define the communication port port = 8080 # Bind the socket to the port sock.bind((host, port)) # Listen for incoming connections sock.listen(1) # Wait for a connection print 'waiting for a connection' connection, client = sock.accept() print client, 'connected' # Receive the data in small chunks and retransmit it data = connection.recv(16) print 'received "%s"' % data if data: connection.sendall(data) else: print 'no data from', client # Close the connection connection.close()
Server SIDE Output
$ python server.py waiting for a connection ('127.0.0.1', 47050) connected received "message"
Client SIDE Output
$ python client.py connecting message socket closed