How to get IP address in Python

Hi..to all …today i learnt how to get IP address in python..

To get the Hostname

import socket
print socket.gethostname()

To get the IP Address

import socket
print socket.gethostbyname(socket.gethostname())

we can also check with dummy socket and see the host name

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
s.getsockname()[0]

Thats it…:)

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

4 thoughts on “How to get IP address in Python”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: