python下获取网卡物理地址

import uuid,socket
import requests,json


dingtalk_hook = "https://oapi.dingtalk.com/robot/send?access_token=5addaea889d44ee8c33376f50688782549d797c3711b1e7d92f8ec2f40f57526"


def get_mac_address(): 
    mac=uuid.UUID(int = uuid.getnode()).hex[-12:] 
    return "-".join([mac[e:e+2] for e in range(0,11,2)])

def get_host_ip():
    """
    查询本机ip地址
    :return: ip
    """
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(('8.8.8.8', 80))
        ip = s.getsockname()[0]
    finally:
        s.close()

    return ip

try:
    myname = socket.getfqdn(socket.gethostname())
    myaddr = socket.gethostbyname(myname)

    mac = get_mac_address().upper()
    addr = get_host_ip().upper()

    print(f"\n您的电脑的物理地址是{mac},\nIP地址是:{addr}")
    u_input = input("请输入您的姓名后按回车:\n")

    print(f"您输入的姓名是{u_input},等待传送数据")

    data = {
        "msgtype":"text",
        "text":{
            "content":f"姓名:{u_input};物理地址是:{mac};IP地址是:{addr}"
        }
    }

    headers = {'content-type': 'application/json'}
    r = requests.post(dingtalk_hook,data=json.dumps(data), headers=headers)

    # print(r.json())

    input("传输完成,按任意键关闭窗口")

except Exception as  e:
    print(str(e))
    print("获取失败,请截图该页面发给安海明")
    input("传输完成,按回车关闭窗口")

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 anaf@163.com
目录