{"id":3592,"date":"2018-07-04T21:24:13","date_gmt":"2018-07-04T15:54:13","guid":{"rendered":"http:\/\/codetheory.in\/?p=3592"},"modified":"2018-07-04T21:24:13","modified_gmt":"2018-07-04T15:54:13","slug":"introduction-to-the-socket-module-in-python","status":"publish","type":"post","link":"https:\/\/codetheory.in\/introduction-to-the-socket-module-in-python\/","title":{"rendered":"Introduction to the socket module in Python"},"content":{"rendered":"

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. <\/p>\n

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.<\/p>\n

\r\nimport socket\r\n<\/pre>\n

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

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.<\/p>\n

\r\nsocket.gethostbyname\r\n<\/pre>\n

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

\r\nsocket.gethostbyname('www.google.com')\r\n<\/pre>\n

Once the above piece of Python code got executed in my own operating system, I got the following output in the interactive console.<\/p>\n

\r\n'216.58.213.196'\r\n<\/pre>\n

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.<\/p>\n

For example, one can easily translate ip addresses to hostnames by making use of the following function.<\/p>\n

\r\nsocket.gethostbyaddr(ip_address_here)\r\n<\/pre>\n

We can easily translate the IPV4 address ‘216.58.213.196’ by making use of the Python code which is being shown below.<\/p>\n

\r\nsocket.gethostbyaddr('216.58.213.196')\r\n<\/pre>\n

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.<\/p>\n

\r\n'ham02s15-in-f196.1e100.net', ['196.213.58.216.in-addr.arpa', 'ham02s15-in-f4.1e100.net'], ['216.58.213.196'])\r\n<\/pre>\n

There are many other useful functionalities to the coder, offered by the Python’s socket module.<\/p>\n

The following finds out if the IPV6 internet protocol is supported by the platform in which the code is being executed.<\/p>\n

\r\nsocket.has_ipv6\r\n<\/pre>\n

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.<\/p>\n

 \r\nTrue\r\n<\/pre>\n

As you can understand, the above boolean value indicates that the IPV6 internet protocol is supported on my own operating system.<\/p>\n

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.<\/p>\n

\r\nsocket.socket(socket_family, socket_type, protocol=0)\r\n<\/pre>\n

Let me explain to you guys each one of the parameters being shown above.<\/p>\n

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.<\/p>\n