Hello friends, I'm Parvez and this is our second video in Python socket programming
So in the previous video we had a simple introduction to socket class in Python and also we have
created this simple example in Python language. So in this video I want to show you that how you can get hostname and also IP address of
your local machine using socket class in Python. So now let's get started
I have opened my PyCharm IDE and I've created a new Python file
Now the first thing I need, I want to import socket like this
And now let me create a method in here. So first I want to get the hostname
So you can get the hostname using a socket.gethostname like this
Also for IP address, let me write IP address, you can use socket class.gethostbyname
And in here you need to give your hostname. Okay now let me just print these two
So print hostname is and you need to give your hostname in here
And print IP address, IP address is .format and this is IP address
So now in here, F underscore name underscore main. Now let me call my main in here
So now you can see that using, we have imported the socket class and in the socket we have
a get hostname and get hostbyname. So the get hostname is for getting our local machine name and this is the IP address that
we can use, hostname.gethostbyname and we can get the IP address. And in here we have printed these
So now I'm going to run this. You can run simply by terminal and now you can see hostname, this is my hostname and
this is the IP address of my local machine like this. So like this you can do and let me just close this and let me open my terminal and from
terminal you can say python, my file name is gethostname.py
Now you can see the result is the same. So thank you guys
I will add the source code to the video description. If you have any question please let me know and for the further videos please subscribe
to my channel and also share the video