Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the external IP of a socket in Python?

Tags:

python

sockets

When I call socket.getsockname() on a socket object, it returns a tuple of my machine's internal IP and the port. However, I would like to retrieve my external IP. What's the cheapest, most efficient manner of doing this?

like image 948
verix Avatar asked Sep 12 '08 04:09

verix


People also ask

How do I find external source IP address?

Type "cmd" in the search box in the Start Menu or taskbar and click the Command Prompt icon to open the Windows command prompt. Type "ipconfig" in the command prompt window and take note of the IP address displayed.

What is external source IP?

An external IP address is the address assigned to you by your ISP (Internet service provider) that the Internet and other computers outside your local network use to identify you.


1 Answers

The most simple method of getting a public IP is by using this

import requests

IP = requests.get('https://api.ipify.org/').text
print(f'Your IP is: {IP}')
like image 94
jamjam46 Avatar answered Oct 26 '22 20:10

jamjam46