[Twisted-Python] Question for obtaining mac-address

Cory Benfield cory at lukasa.co.uk
Thu Nov 26 09:04:20 MST 2015


> On 26 Nov 2015, at 13:29, Pavel Zverev <pzverr at gmail.com> wrote:
> 
> Have 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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: </pipermail/twisted-python/attachments/20151126/69f66750/attachment.sig>


More information about the Twisted-Python mailing list