English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python Simple Method to Get the Public IP Address

This article describes a simple method to obtain the external IP address of the current machine using Python. Share it with everyone for reference, as follows:

#encoding=utf-8
#author: walker
#date: 2016-03-07
#function: Get your external network IP
import requests
from bs4 import BeautifulSoup
# Get external network IP
def GetOuterIP():
  url = r'http://www.whereismyip.com/'
  r = requests.get(url)
  bTag = BeautifulSoup(r.text, 'html.parser', from_encoding='utf-8').find('b')
  ip = ''.join(bTag.stripped_strings)
  print('ip:' + ip)
if __name__ == '__main__':
  GetOuterIP()

Readers who are interested in more content related to Python can check the special topics of this site: 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', 'Classic Tutorial of Python Entry and Progression', and 'Summary of Python File and Directory Operation Skills'.

I hope this article is helpful to everyone in Python program design.

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to notice#w3Please send an email to codebox.com (replace # with @ when sending email) to report any infringement, and provide relevant evidence. Once verified, this site will immediately delete the suspected infringing content.

You May Also Like