0

I want to receive mac address of the clients as soon as they connect to server. For this I have tried sockets but server won't run unless I set a timeout. I want to run sockets the entire time so that I can receive data from client whenever it reaches server. Basically I have a wi-fi setup and want to keep record of clients using their mac address who are connecting to my wi-fi network. Is there any other way to get their mac address apart from ssh. my view.py file is as follows, if there is error please notify.

from django.shortcuts import render

import os
# Create your views here.
#from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
#import socket, sys

import pickle
import socket

TCP_IP = '127.0.0.1'
TCP_PORT = 5005
BUFFER_SIZE = 1024

s = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
s.bind((TCP_IP, TCP_PORT))
s.listen(1)
s.settimeout(3)
try:
    conn, addr = s.accept()
    objrcv = pickle.loads ( conn.recv ( 1024 ) )
    ip = objrcv[0]
    mac = objrcv[1]
except:
    print "Can't connect"

def student_detail(request):
    queryset = attendance.objects.filter(mac=mac)
    context ={
        "object_list":queryset,
        "title": "Attendance",

    }
    return render(request, "attendance.html", context)  
  • 2
    What do you mean by connect to django server? People never connect to it, they send web requests to it – Sayse Jun 16 '16 at 06:51
  • I'm setting up a wi-fi network. I want to get their mac address as soon as they connect to my wi-fi network and display their mac address on django server. Sorry for that....I'm new to django –  Jun 16 '16 at 06:53
  • Django isn't a server and it never will be. From the [docs for `runserver`](https://docs.djangoproject.com/en/1.9/ref/django-admin/#runserver) - "And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers" – Sayse Jun 16 '16 at 06:57
  • @hugh what about tornado? http://www.tornadoweb.org/en/stable/ – doniyor Jun 16 '16 at 06:59

1 Answers1

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.

Community
  • 1
  • 1
ohrstrom
  • 2,890
  • 2
  • 20
  • 34
  • there could be around 4000 clients to my server, will this script have any affect on my resource utilization as there will be lots of processes going on besides this –  Jun 16 '16 at 07:32
  • What do you mean with '4000 clients to my server'? The relevant number here would be how many 'requests per second' you do expect. – ohrstrom Jun 16 '16 at 07:51
  • around 500 requests per second –  Jun 16 '16 at 07:54
  • The mac lookup here should not be the bottleneck. The information comes from the arp table, so it is fast. But serving 500r/s through Django with database queries will need some resources... – ohrstrom Jun 16 '16 at 08:23