2

How can i find my ip address with python not local ip ? I try with socket but it find local ip.I need to ethernet outer ip.That's my code but it find local ip

import socket
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print("Your Computer Name is:" + hostname)
print("Your Computer IP Address is:" + IPAddr)
5
  • Does this answer your question? Getting a machine's external IP address with Python Commented Apr 21, 2020 at 15:45
  • no it find local ip but i want to find like whatismyipaddress.com Commented Apr 21, 2020 at 15:48
  • there is plenty of example in stackoverflow including the one i provided, you can use the search toolbar, plased on top. Commented Apr 21, 2020 at 15:50
  • You can try ipinfo.io/ip Commented Apr 21, 2020 at 15:50
  • and another service ifconfig.me Commented Apr 21, 2020 at 16:51

1 Answer 1

5

I used to have this problem and this was my solution:

you should install this package then:

import requests

...

public_ip = requests.get("http://wtfismyip.com/text").text

print(public_ip)

It's not possible to get the public IP using socket

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.