Python Socket Programing for Beginners

In this Python article we are going to talk about Socket Programing for Beginners in Python,

we will learn how you can create socket in Python, how you can get local machine IP Address,

how you can get website IP Address in Python, how to connect TCP client to server and

different topics.

 

 

 

 

What is Socket ?

a network socket is a virtual end point where entities can perform inter-process

communication. For example, one process sitting in a computer, exchanges data

with another process sitting on the same or another computer. We typically label

the first process which initiates the communication as the client and the another

one as the server. ok now let’s make our first example.

 

 

 

 

How to Create Socket in Python

You can use socket.socket() for creating socket in Python, and you need to give some

parameters.

 

 

Family : This is the domain of socket, such as AF_INET (about

90 percent of the sockets of the Internet fall under this category) or

AF_UNIX, which is sometimes used as well. In Python 3, you can create a

Bluetooth socket using AF_BLUETOOTH.

 

Socket type: Depending on your need, you need to specify the type of socket.

For example, TCP and UDP-based sockets are created by specifying SOCK_STREAM

and SOCK_DGRAM, respectively.

 

Protocol: This specifies the variation of protocol within a socket family

and type. Usually, it is left as zero.

 

 

Run the code and this is the result.

Python Socket Creation
Python Socket Creation

 

 

How to Get Local Machine IP Address Using Socket

Sometimes, you need to quickly discover some information about your machine,

for example, the host name, IP address, number of network interfaces, and so on.

this is very easy to achieve using Python and Socket. so this is the complete code 

for this example.

 

 

We call the gethostname() method from the socket library for the host name and for IP

Address we use gethostbyname().

 

 

In the above code we have imported socket one of Python’s core networking libraries.

then, we have used the two utility functions, gethostname() and gethostbyname(host_name).

You can type help(socket.gethostname) to see the online help information from within the

command line.

 

 

Run the complete code and this is the result.

Python Socket Local Machine IP Address
Python Socket Local Machine IP Address

 

 

 

How to Get Website IP Address in Python Socket 

Sometimes, you need to translate a machine’s hostname into its corresponding IP

Address, for example, a quick domain name lookup. this section introduces a simple function

to do that. If you need to know the IP Address of a Website, you can use a built-in library 

function, gethostbyname(). In this case, you need to pass the remote hostname as its parameter.

 

 

Run the complete code and this is the result.

Python Socket Programing for Beginners
Python Socket Programing for Beginners

 

 

How to Connect TCP Client Socket in Python

In this part we want to connect TCP client socket in Python, so now this is the complete

code for this section. the following code is an example of TCP client socket that makes a

connection to server socket.

 

 

 

 This line of code is for creating socket in python programming language , also we are

handling exceptions for this.

 

 

Also in here we are going to get the input of host name and port from the user.

 

 

In here we are going to connect our target host to the port.

 

 

 

Run the code give host and port target, this will be the result.

Python Socket TCP client
Python Socket TCP client

 

 

 

How to Convert IP Address in Python 

When you would like to deal with low-level network functions, sometimes, the usual

string notation of IP addresses are not very useful. They need to be converted to the

packed 32-bit  binary formats. the Python socket library has utilities to deal with the

various IP address formats. Here,  we will use two of them, inet_aton() and inet_ntoa().

 

The two IP addresses have been converted from a string to a 32-bit packed format using a

for-in statement. Additionally, the Python hexlify function is called from the binascii module.

This helps to represent the binary data in a hexadecimal format.

 

 

Run the code and this is the result

Python Socket Convert IP
Python Socket Convert IP

 

 

 

How to set Socket Timeout in Python

Sometimes, you need to manipulate the default values of certain properties of a socket

library, for example, the socket timeout. You can make an instance of a socket object and

call a gettimeout() method to get the default timeout value and the settimeout() method

to set a specific timeout value. This is very useful in developing custom server applications.

We first create a socket object inside a socket_time_out() function. Then, we can use the

getter/setter instance methods to manipulate timeout values.

 

 

 

 

Run the code and this is the result.

Python Socket Timeout
Python Socket Timeout

 

Leave a Comment