How can I find the MAC address of a client on the same network, using Python-Flask? How can I find the MAC address of a client on the same network, using Python-Flask? flask flask

How can I find the MAC address of a client on the same network, using Python-Flask?


Not sure but you can always get IP address using the request object like

request.remote_addr and for that you have to

import request and then you can pass this IP to this function

import netifaces as nifdef mac_for_ip(ip):    'Returns a list of MACs for interfaces that have given IP, returns None if not found'    for i in nif.interfaces():        addrs = nif.ifaddresses(i)        try:            if_mac = addrs[nif.AF_LINK][0]['addr']            if_ip = addrs[nif.AF_INET][0]['addr']        except IndexError, KeyError: #ignore ifaces that dont have MAC or IP            if_mac = if_ip = None        if if_ip == ip:            return if_mac    return None

from kursancew's answer to Getting MAC Address.


If you get the gateway then you'll get the mac address corresponding to the interface that is using that gateway:

>>> gateway_iface = netifaces.gateways()['default'][netifaces.AF_INET][1]>>> netifaces.ifaddresses(gateway_iface)[netifaces.AF_LINK][0]['addr']'de:f2:fe:66:a4:8e'