[Fixed]-How to communicate between client and server?

1👍

You ‘know’ the ip address of the connecting client. So you can use this information to obtain more information, e.g. with arp -n

# your_app/views.py
import re
from subprocess import Popen, PIPE
from django.shortcuts import render

def mac(request):

    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
        ip = x_forwarded_for.split(',')[0]
    else:
        ip = request.META.get('REMOTE_ADDR')

    proc = Popen(["arp", "-n", ip], stdout=PIPE)
    out = proc.communicate()[0]
    mac = re.search(r"(([a-f\d]{1,2}\:){5}[a-f\d]{1,2})", out).groups()[0]

    return render(request, 'mac.html', {'mac': mac})

Credits:

Note:

arp -n only gives a result for ‘known’ entries (arp -a). In the situation here it is working because the client request happening before the arp lookup.

Leave a comment