French Noodles
French Noodles

Reputation: 124

Python detect new connection to wifi

I saw a tutorial on YouTube(I can't link it because I can't find it anymore), So the code is supposed to detect devices that are connected to my Internet/Router, I don't understand a lot about how his(The person who made the tutorial) code works

I also got this error in my console:

enter image description here

File "c:/Users/j/Desktop/Connection-Detection.py", line 6, in IP_NETWORK = config('IP_NETWORK') File "C:\Users\j\AppData\Local\Programs\Python\Python38-32\lib\site-packages\decouple.py", line 199, in call return self.config(*args, **kwargs) File "C:\Users\j\AppData\Local\Programs\Python\Python38-32\lib\site-packages\decouple.py", line 83, in call return self.get(*args, **kwargs) File "C:\Users\j\AppData\Local\Programs\Python\Python38-32\lib\site-packages\decouple.py", line 68, in get raise UndefinedValueError('{} not found. Declare it as envvar or define a default value.'.format(option)) decouple.UndefinedValueError: IP_NETWORK not found. Declare it as envvar or define a default value. PS C:\Users\j\Desktop\python\login>

That's "Detection.py"

import sys
import subprocess
import os
from decouple import config

IP_NETWORK = config('IP_NETWORK')
IP_DEVICE = config('IP_DEVICE')

proc = subprocess.Popen(['ping', IP_NETWORK],stdout=subprocess.PIPE)

while True:
    line = proc.stdout.readline
    if not line:
        break
    connected_ip = line.decode('utf-8').split()[3]

    if connected_ip == IP_DEVICE:
        subprocess.Popen(['say', 'Someone connected to network'])



  

Upvotes: 3

Views: 535

Answers (1)

theSekyi
theSekyi

Reputation: 530

You need to define an environment variable in same directory as the Detection.py file.

Steps

  1. Install python-decouple - pip install python-decouple.
  2. Create a file called .env
  3. Open the .env file and paste the following into it.
IP_NETWORK=YOUR_IP_NETWORK
IP_DEVICE=YOUR_IP_DEVICE

Replace YOUR_IP_NETWORK and YOUR_IP_DEVICE with your IP_NETWORK and IP_DEVICE

Upvotes: 1

Related Questions