Skip to content

Instantly share code, notes, and snippets.

@tikismoke
Created November 16, 2016 13:08
Show Gist options
  • Save tikismoke/4a0ee6b4a839594aa949c79f58c6e3cd to your computer and use it in GitHub Desktop.
Save tikismoke/4a0ee6b4a839594aa949c79f58c6e3cd to your computer and use it in GitHub Desktop.
from pydhcplib.dhcp_network import *
from urllib2 import Request, urlopen, URLError, HTTPError #pour pouvoir appeler une url
import time
def do_something():
print("button has been pressed")
urlopen("http://192.168.0.1:40406/rest/cmd/id/291?value=1" , timeout = 1)
# Wait for 2 seconds
time.sleep(2)
urlopen("http://192.168.0.1:40406/rest/cmd/id/291?value=0" , timeout = 1)
netopt = {'client_listen_port':"68", 'server_listen_port':"67", 'listen_address' :"0.0.0.0"}
class Server(DhcpServer):
def __init__(self, options, dashbuttons):
DhcpServer.__init__(self, options["listen_address"],
options["client_listen_port"],
options["server_listen_port"])
self.dashbuttons = dashbuttons
def HandleDhcpRequest(self, packet):
mac = self.hwaddr_to_str(packet.GetHardwareAddress())
self.dashbuttons.press(mac)
def hwaddr_to_str(self, hwaddr):
result = []
hexsym = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f ']
for iterator in range(6) :
result += [str(hexsym[hwaddr[iterator]/16]+hexsym[hwaddr[iterator]%1 6])]
return ':'.join(result)
class DashButtons():
def __init__(self):
self.buttons = {}
def register(self, mac, function):
self.buttons[mac] = function
def press(self, mac):
if mac in self.buttons:
self.buttons[mac]()
return True
return False
dashbuttons = DashButtons()
dashbuttons.register("xx:xx:xx:xx:xx:xx", do_something)
server = Server(netopt, dashbuttons)
while True :
server.GetNextDhcpPacket()
@tikismoke
Copy link
Author

Works only for 1 dash for now
And needed mac in minus at the end of file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment