Get IP, Hostname,Port , Protocol details using python

Get IP address,  Hostname,Port , Protocol details of your machine using python

This is all the magic of  sockets  provided by BSD socket interface

Import sockets

>>>import socket

To fetches address information for a  TCP connection to website eg, google.com
The output corresponds to  (family, socktype, proto, canonname, sockaddr) 

>>> socket.getaddrinfo("www.google.com", 80, 0, 0, socket.SOL_TCP)
[(2, 1, 6, '', ('74.125.239.17', 80)),   
 (2, 1, 6, '', ('74.125.239.18', 80)),
 (2, 1, 6, '', ('74.125.239.20', 80)),
 (2, 1, 6, '', ('74.125.239.19', 80)),
 (2, 1, 6, '', ('74.125.239.16', 80)),
 (10, 1, 6, '',('2607:f8b0:4007:801::1013', 80, 0, 0))]

ie (family, socktype, proto, canonname, sockaddr)
   (2, 1, 6, '', ('74.125.239.18', 80)Respectively map it

The last tuple is IPV6 (10, 1, 6, '',('2607:f8b0:4007:801::1013', 80, 0, 0))

To get hostname of ur machine
>>> socket.gethostname()
'ubu10-04-64'

To get IP address 
>>> socket.gethostbyname(socket.gethostname())
'10.10.3.6'

To get Full qualified domain name (fqdn)
>>> socket.gethostbyaddr('10.100.3.76')
('ubu10-04-64.xxx.xxxx.com', ['ubu10-04-64'], ['10.100.3.76'])

Get protocol constant mapping number
>>> socket.getprotobyname('ICMP')
1
>>> socket.getprotobyname('TCP')
6

To Translate an Internet port number and protocol name to a service name for that service
>>> socket.getservbyport(53)
'domain'
ie. DNS service (Domain name resolution)

>>> socket.getservbyport(80)
'www'