Get your own IP address in Python

As the title says. I have confirmed the operation on windows and mac. You can get it as an array.

Package installation

pip install psutil
pip install netifaces

script

import netifaces as ni
import psutil
import os
import socket


def get_ip() -> list:
    if os.name == "nt":
        # Windows
        return socket.gethostbyname_ex(socket.gethostname())[2]
        pass
    else:
        #other than that
        result = []
        address_list = psutil.net_if_addrs()
        for nic in address_list.keys():
            ni.ifaddresses(nic)
            try:
                ip = ni.ifaddresses(nic)[ni.AF_INET][0]['addr']
                if ip not in ["127.0.0.1"]:
                    result.append(ip)
            except KeyError as err:
                pass
        return result


print(get_ip())

result

['192.168.XXX.XXX', '10.211.XX.X', '10.37.XXX.X']

Reference site

https://stackoverflow.com/questions/24196932/how-can-i-get-the-ip-address-from-nic-in-python https://stackoverflow.com/questions/3837069/how-to-get-network-interface-card-names-in-python https://edosha.hatenablog.jp/entry/2017/08/09/150636

Recommended Posts

Get your own IP address in Python
[Python] logging in your own module
Get the client's IP address in Django
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Easily use your own functions in Python
Get local IP address
Get date in Python
Import your own modules in Grasshopper's Python development
Create your own Big Data in Python for validation
Get last month in python
Create your own Random Dot Stereogram (RDS) in Python.
Try to improve your own intro quiz in Python
[Python] Insert ":" in MAC address
Use the CASA Toolkit in your own Python environment
Get Terminal size in Python
Explicitly get EOF in python
[Road to intermediate Python] Define in in your own class
Get Evernote notes in Python
Get Japanese synonyms in Python
Get your heart rate from the fitbit API in Python!
Try sorting your own objects with priority queue in Python
About netaddr that operates IP address, CIDR, etc. in Python
Get Leap Motion data in Python.
[Python] Make your own LINE bot
Get data from Quandl in Python
Get the desktop path in Python
Get the script path in Python
Get, post communication memo in Python
Hash method (open address method) in Python
Get the desktop path in Python
Get the host name in Python
Get started with Python in Blender
[OCI] Python script to get the IP address of a compute instance in Cloud Shell
Create your own graph structure class and its drawing in python
Specify your own class in class argument and return type annotation in Python
Get additional data in LDAP with python
Get exchange rates from open exchange rates in Python
Get Suica balance in Python (using libpafe)
[Python] Package and distribute your own modules
Get Google Fit API data in Python
[Python] Register your own library on PyPI
Get Youtube data in Python using Youtube Data API
Until you install your own Python library
To get a local IP address programmatically
Get battery level from SwitchBot in Python
Get a token for conoha in python
Publish your own Python library with Homebrew
Try text mining your diary in Python
Get the EDINET code list in Python
Get Cloud Logging available in Python in 10 minutes
Get rid of DICOM images in Python
Get metric history from MLflow in Python
Get a list of packages installed in your current environment with python
Make your own NOAA / GOES familiar X-ray flux 3days plot in Python
Get time series data from k-db.com in Python
Quadtree in Python --2
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python