In Python Network Programming there are two levels of access to network services. Low level access and High level access.
At a low level, the basic socket support can be accessed in the underlying operating system. It allows you to implement clients and servers for both connection-oriented and connectionless protocols.
Higher level libraries provide access to specific application-level network protocols, such as FTP, HTTP, and many more.
Let’s know about sockets in python network programming.
Sockets:
In python, Sockets are the endpoints of a bidirectional communications channel. Sockets can communicate within a process, between processes on the same machine, or between processes on different continents. The socket library provides specific classes for handling the common transports as well as a generic interface for handling the rest. Sockets can be implemented over a number of different channel types: Unix domain sockets, TCP, UDP, and many more.
Sockets contain their own vocabulary as shown in the given table below:
Terms | Description |
Domain | The family of protocols 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. |
type | The type of communications between the two endpoints, typically SOCK_STREAM for connection-oriented protocols and SOCK_DGRAM for connectionless protocols. |
hostname | The identifier of a network interface − A string, which can be a hostname, a dotted-quad address, or an IPV6 address in colon (and possibly dot) notation A string “<broadcast>”, which specifies an INADDR_BROADCAST address. A zero-length string, which specifies INADDR_ANY, or An Integer interpreted as a binary address in host byte order. |
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. |
The Socket Module:
To create a socket in python, there must use the socket.socket() function available in socket module.
Syntax:
Parameters used in the syntax:
- socket_family: This is either AF_UNIX or AF_INET, as explained earlier.
- protocol: This is usually left out, defaulting to 0.
- socket_type: This is either SOCK_STREAM or SOCK_DGRAM.
Server Socket Methods:
Method | Description |
s.bind() | This method binds address (hostname, port number pair) to socket. |
s.accept() | This passively accept TCP client connection, waiting until a connection arrives (blocking). |
s.listen() | This method sets up and starts a TCP listener. |
Client Socket Methods:
Method | Description |
s.connect() | This method actively initiates TCP server connection. |
General Socket Methods:
Methods | Description |
s.recv() | This method receives a TCP message. |
s.recvfrom() | This method receives a UDP message. |
s.close() | This method closes the socket. |
s.sendto() | This method transmits UDP messages. |
s.send() | This method transmits TCP messages. |
socket.gethostname() | Returns the hostname. |
Creating Simple Server:
To write Internet servers in the python network program, the socket function used which is available in the socket module to create a socket object. A socket object is then used to call other functions to set up a socket server.
Now call bind(hostname, 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 specified, and then returns a connection object that represents the connection to that client.
#python echo_server.py import socket host = '' # Symbolic name meaning all available interfaces port = 12345 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(1) conn, addr = s.accept() print('Connected by', addr) while True: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close()
Creating Simple Client:
The socket.connect(hostname, port ) opens a TCP connection to the hostname on the port. Once a socket has opened, you can read from it like any other IO object. When done, remember to close it, as you would close a file.
The code given below is a simple client program that connects to a given host and port, reads any available data from the socket, and then exits.
# echo_client.py import socket host = socket.gethostname() port = 12345 # The same port as used in the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.sendall(b'Python, client') data = s.recv(1024) s.close() print('Received', repr(data))
Above server ,client will show following output :
Python Internet modules:
List of some important modules in Python Network/Internet programming are given below;
Protocol | Common function | Port No | Python module |
POP3 | Fetching email | 110 | poplib |
SMTP | Sending email | 25 | smtplib |
Telnet | Command lines | 23 | telnetlib |
IMAP4 | Fetching email | 143 | imaplib |
Gopher | Document transfers | 70 | gopherlib, urllib |
NNTP | Usenet news | 119 | nntplib |
HTTP | Web pages | 80 | httplib, urllib, xmlrpclib |
FTP | File transfers | 20 | ftplib, urllib |
Unix Socket Programming:
As mentioned earlier, Sockets are communication points on the same or different computers to exchange data. Sockets are supported by Unix, Windows, Mac, and many other operating systems. This article will provide a strong foundation because it’s covering basic topics such as network addresses, hostnames, architecture, ports, and services before moving into network address functions and explaining how to write client/server codes using sockets.