mirror of
https://github.com/ThunixdotNet/thunix_api.git
synced 2026-06-25 11:19:24 +00:00
Refractor thunix_api to use flask_rest package
This commit is contained in:
32
endpoints/ip_info.py
Normal file
32
endpoints/ip_info.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from flask_restful import Resource
|
||||
import psutil
|
||||
|
||||
class Ip_Info(Resource):
|
||||
def get_ip_addresses(self, family):
|
||||
ip_addresses = []
|
||||
for interface, snics in psutil.net_if_addrs().items():
|
||||
for snic in snics:
|
||||
if snic.family == family:
|
||||
ip_addresses.append(
|
||||
{
|
||||
# We use caps against convention here to make it easier to append
|
||||
# into the JSON payload
|
||||
"Interface": interface,
|
||||
"Address": snic.address,
|
||||
"Netmask": snic.netmask
|
||||
}
|
||||
)
|
||||
return ip_addresses
|
||||
|
||||
def get(self):
|
||||
ipv4 = self.get_ip_addresses(socket.AF_INET)
|
||||
|
||||
payload = [
|
||||
{
|
||||
"Interfaces": []
|
||||
}
|
||||
]
|
||||
for addr in ipv4:
|
||||
payload[0]["Interfaces"].append(addr)
|
||||
|
||||
return payload
|
||||
Reference in New Issue
Block a user