Introduction to the socket module in Python

Almost every computer programming language offers already builtin modules which help to script networking functionalities. Based on my experience with the Python computer technology, socket is a very useful module when it comes to interaction with the networking interface. According to the official documentation, the socket module is available on all modern Unix systems, Windows, Mac OS X, BeOS, OS/2 and probably additional platforms.

Through this article, you will learn some basics of the Python’s socket module. Before going any further make sure to launch a new Python interactive console in your own operating system. Once you have managed to do that, make use of the import statement like shown below.

import socket

As you may already know, before we can make use of a module in the Python computer programming language, we have to import it.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

Now that you have imported the Python’s socket module, it is time to get our hands dirty. The first functionality which we are going to explore, is the one which helps to translate a hostname to the IPv4 address format.

socket.gethostbyname

The above builtin function, part of the Python’s socket module, takes as an input a hostname and translates it to an IPV4 address.

socket.gethostbyname('www.google.com')

Once the above piece of Python code got executed in my own operating system, I got the following output in the interactive console.

'216.58.213.196'

Not only does the Python’s socket module offer one the ability to translate hostnames to ip addresses, but it also offers functionalities to accomplish the reverse process.

For example, one can easily translate ip addresses to hostnames by making use of the following function.

socket.gethostbyaddr(ip_address_here)

We can easily translate the IPV4 address ‘216.58.213.196’ by making use of the Python code which is being shown below.

socket.gethostbyaddr('216.58.213.196')

According to the official documentation, the socket.gethostbyaddr function, returns a triple. Once the above piece of Python code got executed on my own operating system, I got the following output.

'ham02s15-in-f196.1e100.net', ['196.213.58.216.in-addr.arpa', 'ham02s15-in-f4.1e100.net'], ['216.58.213.196'])

There are many other useful functionalities to the coder, offered by the Python’s socket module.

The following finds out if the IPV6 internet protocol is supported by the platform in which the code is being executed.

socket.has_ipv6

According to the Python’s official documentation, the socket.has_ipv6 function returns a boolean value. Once the above piece of Python code got executed in my own operating system, I got the following output.

 
True

As you can understand, the above boolean value indicates that the IPV6 internet protocol is supported on my own operating system.

Now let’s try to create some sockets. Based on my personal experience with sockets in the Python computer programming language, the following syntax should be used to create one.

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

Let me explain to you guys each one of the parameters being shown above.

As far as my knowledge goes with sockets in the Python computer programming language, the socket family can be one of the two shown below.

  • AF_UNIX
  • AF_INET

The socket_family parameter in the above function stands for the protocols used as transport mechanisms. What about the socket_type parameter?

It stands for the type of connection, connection oriented protocols or connectionless protocols. As for the third parameter, we are going to leave it as default.

For now, let’s create a fresh socket by making use of the Python code which is being shown below.

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

Once you have managed to write the above piece of Python code, then bind address to socket like shown below.

s.bind(('127.0.0.1', 13)) # binds address to socket

Once finished with binding, setup the TCP listener with the help of the Python code shown below.

s.listen(5)

Now that we have prepared the TCP listener, it is time to write a while True loop with the main purpose of accepting connections from clients.

Type the following code in your interactive console.

while True:
    c, addr = s.accept()
    print('Got connection from', addr)
    c.send('Welcome to the TCP listener')
    c.close()  # close the connection

Hit the return button on your keyboard and let the TCP listener to listen for incoming connections.

Now open a new window on your terminal application, and launch a fresh Python interactive console. It is time to code the client.

First, import the socket module by making use of the Python code which is being shown below.

import socket

Then create a fresh socket for the client by making use of the Python code shown below.

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = '127.0.0.1'
port = 13

Once finished with setting up a new socket for the client, make use of the connect() method to connect to the TCP listener.

client.connect((host, port))

Once the connection is being made, go to the terminal window where the TCP listener is; you will see the following output in there.

('Got connection from', ('127.0.0.1', 63068))

Then go to the terminal window of the client, and type the following piece of Python code to receive some data.

client.recv(1024)

Once the above piece of Python code got executed on my own operating system, I got the following output.

'Thank you for connecting'

Final thoughts

There are many other functionalities offered by the Python’s socket module which can be easily put to use with the main purpose of writing computer networking applications. Through this article, we covered some basics, so you can get ready for writing a more complex client and server with the help of the Python’s socket module.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *