Post by Pavel ZverevHave nice day, developers!
I am a beginner python developer, and recently I came across the Twisted framework, I was interested in whether I get the MAC-address of the client connected to the network, similar twisted.web.http.Request.getClientIP.
If youâre trying to get the MAC address of the client from an IP connection, then this is basically not possible. Once the packet has been handled as an IP packet, the MAC address is simply not used anymore. This is primarily because IP packets can cross layer 3 boundaries, so the MAC address from which a given IP packet arrived is potentially entirely unrelated to the MAC address of the source machine.
Generally speaking, I do not believe the socket layer provides any mechanism for obtaining the MAC address for the remote peer on a socket of type AF_INET: itâs just not relevant information.
Assuming youâre restricting yourself to the case where the client is on the same layer 2 broadcast domain as the server (again, hardly a guaranteed state of affairs), then the easiest way to obtain the MAC address is actually to grab it out of your machineâs ARP cache. If you execute `arp -a` (either in a shell or a Python subprocess), you will get access to the ARP cache on your machine which you can use to locate the MAC address for a given IP. This of course is IPv4 specific (ARP does not exist for IPv6): for IPv6 youâd want to use `ip -6 neigh show` (`ip neigh show` also works for ARP but provides quite a lot of info).
Cory