<p>When I call <code>socket.getsockname()</code> 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?</p>

[{'answer_id': 58296, 'author': 'John Millikin', 'author_id': 3560, 'author_profile': 'https://Stackoverflow.com/users/3560', 'pm_score': 4, 'selected': True, 'text': "<p>This isn't possible without cooperation from an external server, because there could be any number of NATs between you and the other computer. If it's a custom protocol, you could ask the other system to report what address it's connected to.</p>\n"}, {'answer_id': 58299, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 3, 'selected': False, 'text': '<p>The only way I can think of that\'s guaranteed to give it to you is to hit a service like <a href="http://whatismyip.com/" rel="noreferrer">http://whatismyip.com/</a> to get it.</p>\n'}, {'answer_id': 256358, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>import socket</p>\n\n<p>s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)</p>\n\n<p>s.connect(("msn.com",80))</p>\n\n<p>s.getsockname()</p>\n'}, {'answer_id': 2646313, 'author': 'frankjania', 'author_id': 317610, 'author_profile': 'https://Stackoverflow.com/users/317610', 'pm_score': 0, 'selected': False, 'text': '<p>Using the address suggested in the source of <a href="http://whatismyip.com" rel="nofollow noreferrer">http://whatismyip.com</a></p>\n\n<pre><code>import urllib\ndef get_my_ip_address():\n    whatismyip = \'http://www.whatismyip.com/automation/n09230945.asp\'\n    return urllib.urlopen(whatismyip).readlines()[0]\n</code></pre>\n'}, {'answer_id': 8305740, 'author': 'Rafael Lopes', 'author_id': 1070554, 'author_profile': 'https://Stackoverflow.com/users/1070554', 'pm_score': 3, 'selected': False, 'text': '<p><a href="https://github.com/bobeirasa/mini-scripts/blob/master/externalip.py" rel="noreferrer">https://github.com/bobeirasa/mini-scripts/blob/master/externalip.py</a></p>\n\n<pre><code>\'\'\'\nFinds your external IP address\n\'\'\'\n\nimport urllib\nimport re\n\ndef get_ip():\n    group = re.compile(u\'(?P&lt;ip&gt;\\d+\\.\\d+\\.\\d+\\.\\d+)\').search(urllib.URLopener().open(\'http://jsonip.com/\').read()).groupdict()\n    return group[\'ip\']\n\nif __name__ == \'__main__\':\n    print get_ip()\n</code></pre>\n'}, {'answer_id': 8848856, 'author': 'Vasilii Pascal', 'author_id': 1147450, 'author_profile': 'https://Stackoverflow.com/users/1147450', 'pm_score': 1, 'selected': False, 'text': "<pre><code>print (urllib.urlopen('http://automation.whatismyip.com/n09230945.asp').read())\n</code></pre>\n"}, {'answer_id': 49715732, 'author': 'Scott', 'author_id': 9614384, 'author_profile': 'https://Stackoverflow.com/users/9614384', 'pm_score': 2, 'selected': False, 'text': '<p>You\'ll need to use an external system to do this.</p>\n\n<p>DuckDuckGo\'s IP answer will give you exactly what you want, and in JSON!</p>\n\n<pre><code>import requests\n\ndef detect_public_ip():\n    try:\n        # Use a get request for api.duckduckgo.com\n        raw = requests.get(\'https://api.duckduckgo.com/?q=ip&amp;format=json\')\n        # load the request as json, look for Answer.\n        # split on spaces, find the 5th index ( as it starts at 0 ), which is the IP address\n        answer = raw.json()["Answer"].split()[4]\n    # if there are any connection issues, error out\n    except Exception as e:\n        return \'Error: {0}\'.format(e)\n    # otherwise, return answer\n    else:\n        return answer\n\npublic_ip = detect_public_ip()\nprint(public_ip)\n</code></pre>\n'}, {'answer_id': 54043521, 'author': 'Ahmed', 'author_id': 10679414, 'author_profile': 'https://Stackoverflow.com/users/10679414', 'pm_score': 0, 'selected': False, 'text': '<hr>\n\n<p>You need to make connection to an external Server And Get Your Public IP From The Response</p>\n\n<hr>\n\n<p><strong>like this</strong>:</p>\n\n<pre><code>   import requests\n\n   myPublic_IP = requests.get("http://wtfismyip.com/text").text.strip()\n\n   print("\\n[+] My Public IP: "+ myPublic_IP+"\\n")\n</code></pre>\n'}, {'answer_id': 66517656, 'author': 'jamjam46', 'author_id': 14914535, 'author_profile': 'https://Stackoverflow.com/users/14914535', 'pm_score': 1, 'selected': False, 'text': "<p>The most simple method of getting a public IP is by using this</p>\n<pre><code>import requests\n\nIP = requests.get('https://api.ipify.org/').text\nprint(f'Your IP is: {IP}')\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/58294', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5342/']