获取mac地址,内网ip,当前ip位置信息

复制代码
import json
import socket
import time
import requests
from urllib.parse import quote

class Home_Url():
    def get_mac(self):
        from psutil import net_if_addrs
        mac = ''
        info = net_if_addrs()
        for k, v in info.items():
            if k not in ['以太网', 'en0'] and '以太网' not in str(k):
                continue
            print(k)
            for i in v:
                if '-' in i[1] or ':' in i[1]:
                    if len(i[1]) == 17:
                        mac = str(i[1]).lower()
                        break
        if not mac:
            node = uuid.getnode()
            mac = uuid.UUID(int=node).hex[-12:]
            mac = '-'.join([mac[i:i + 2] for i in range(0, len(mac) + 2 // 2, 2)])[:-1]
        return mac

    def get_external_ip(self):
        external_ip = None
        url1 = 'https://ipinfo.io/json'
        url2 = 'https://checkip.amazonaws.com/json'
        url3 = 'https://api.ipify.org/?format=json'
        url4 = 'https://api64.ipify.org'
        url5 = 'http://ip-api.com/json/?lang=zh-CN'
        urls = [url1, url2, url4, url3, url5]
        headers = {
            'Accept': '*/*',
            'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36 Edg/117.0.2045.47'
        }
        for url in urls:
            try:
                proxies = {"http": None, "https": None}
                res = requests.get(url, headers=headers, proxies=proxies, timeout=25)
                if res.text.count('{') > 0:
                    response = json.loads(res.text)
                    external_ip = response.get('ip') or response.get('query')
                else:
                    external_ip = res.text
                if external_ip.strip():
                    return external_ip.strip()
            except:
                pass
        return external_ip

    def get_city_region(self):
        # 获取当前ip位置信息
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
        }
        url = 'https://whois.pconline.com.cn/ipJson.jsp?json=true'
        try:
            res = requests.get(url, headers=headers, timeout=5)
            response = json.loads(res.text)
            city = response['city']
            return city
        except:
            return ''

    def get_mac_address(self):
        mac = self.get_mac()
        print('mac:', mac)
        # 获取主机名
        hostname = socket.gethostname()
        # 内网IP地址
        # intranet_ip = socket.gethostbyname(hostname)
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        intranet_ip = s.getsockname()[0]
        # 外网ip
        try:
            external_ip = self.get_external_ip()
        except:
            external_ip = '127.0.0.1'
        return mac, hostname, intranet_ip, external_ip
   
    def run(self):
        mac, hostname, intranet_ip, external_ip = self.get_mac_address()

if __name__ == '__main__':
    start = Home_Url()
    start.run()
相关推荐
牛奔8 小时前
Linux 的日志分析命令
linux·运维·服务器·python·excel
电化学仪器白超8 小时前
20251209Ver8(精密电流源温漂特性测试报告)
python·单片机·嵌入式硬件·自动化
昵称已被吞噬~‘(*@﹏@*)’~9 小时前
【强化学习】MacOS (M1芯片)上最新版本 MuJoCo 通用安装教程(最简洁),PS:不是 mujoco_py 的老版本
python·macos·机器学习·强化学习·mujoco
沃斯堡&蓝鸟9 小时前
DAY33 类的装饰器
开发语言·python
Tipriest_9 小时前
Python构建包,打包.whl文件,使用.whl文件安装包指南
python·whl
BoBoZz199 小时前
ColorEdges 动态有向图的动态渲染
python·vtk·图形渲染·图形处理
彼岸花开了吗9 小时前
构建AI智能体:六十九、Bootstrap采样在大模型评估中的应用:从置信区间到模型稳定性
人工智能·python·llm
echo的PHP开发9 小时前
如何获取苹果手机的 XcodeOrgId
python·智能手机·苹果手机
程序员杰哥9 小时前
Postman设置接口关联,实现参数化
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
沛沛老爹9 小时前
2025年java总结:缝缝补补又一年?
java·开发语言·人工智能·python·guava·总结·web转型ai