4

I am converting bash code to python code. I got a global ip address of the own host in bash code by

hostname -I  # output -> 19x.xxx.xxx.xxx xxxx:xxxx:....

The 19x.xxx.xxx.xxx is the global ip address of the own host.

I tried to get the global ip address in python by

import socket
name=socket.gethostname()
id_address=socket.gethostbyname(name)
print("id_address = {0}".format(id_address))

The output was the local host address like

127.xxx.xxx.xxx

Do I have a way to get a global ip address in python?

mora
  • 2,217
  • 4
  • 22
  • 32
  • that's an interesting -I flag for hostname, it doesn't appear to be available on BSD or OS X, can you tell me which program you are using? If so, we might be able to identify what you need to do in python. Also, if necessary you can call a shell command in python – jmercouris Aug 28 '17 at 15:36
  • @jmercouris: thank you for commenting it. I am using 4.4.0-22-generic #39-Ubuntu. I can call 'hostname -I' at bash shell terminal. Is this the answer you want? I am afraid I can't get what you really want because I am not expert of this field. – mora Aug 28 '17 at 15:41
  • 1
    This may help: https://stackoverflow.com/a/30990617/6394138 – Leon Aug 28 '17 at 15:48
  • The `-I` flag from the man page: *Display all network addresses of the host. This option enumerates all configured addresses on all network interfaces. The loopback interface and IPv6 link-local addresses are omitted. Contrary to option -i, this option does not depend on name resolution. Do not make any assumptions about the order of the output.* – VPfB Aug 28 '17 at 15:50
  • Bearing in mind that 'global ip' is a subjective term, a possible duplicate? [How can I get the IP address of eth0 in Python?](https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-of-eth0-in-python) – match Jan 14 '18 at 21:41

2 Answers2

2

You can do it without any external libraries:

import urllib.request

external_ip = urllib.request.urlopen('https://ident.me').read().decode('utf8')

print(external_ip)

This uses a website that gives you your public ipv4 address using only the standard library, which is great.

Code is tested on python3.

Similar question: Getting a machine's external IP address with Python

Similar answer: https://stackoverflow.com/a/41432835/14154066

thisisrandy
  • 2,660
  • 2
  • 12
  • 25
  • Note that this will return your "preferred" IP address, whether IPv4 or IPv6. You'd have to use v4.ident.me for IPv4 only; more details @ https://api.ident.me – Pierre Carrier Feb 24 '22 at 15:46
1

You have to use a computer/server on the internet to give this information to you. Your own computer can only give you the network card's IP address on the local network, e.g. something like 192.16.8..

You can use the whatismyip module to get your external, public IP address. It has no dependencies outside the Python 3 standard library. It connects to public STUN servers and what-is-my-ip websites to find the IPv4 or IPv6 address. Run pip install whatismyip

Example:

>>> import whatismyip
>>> whatismyip.amionline()
True
>>> whatismyip.whatismyip()  # Prefers IPv4 addresses, but can return either IPv4 or IPv6.
'69.89.31.226'
>>> whatismyip.whatismyipv4()
'69.89.31.226'
>>> whatismyip.whatismyipv6()
'2345:0425:2CA1:0000:0000:0567:5673:23b5'
Al Sweigart
  • 11,566
  • 10
  • 64
  • 92