Created
December 23, 2018 08:42
-
-
Save Yepoleb/4086582fabaf5a0b968f560a8ccb0126 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Script for emulating the Canon BJNP discover request | |
# License: CC0 | |
import socket | |
import time | |
import itertools | |
import time | |
import select | |
from datetime import datetime | |
ip = "192.168.217.255" | |
port = 8611 | |
wait = 3 | |
payload = b"BJNP\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) | |
sock.setblocking(False) | |
sock.bind(("0.0.0.0", 0)) | |
for counter in itertools.count(1): | |
sock.sendto(payload, (ip, port)) | |
sent_time = datetime.now() | |
print(sent_time.strftime("%H:%M:%S"), "Sent packet", counter) | |
selr, selw, selx = select.select([sock], [], [], wait) | |
if not selr: | |
print("Timed out") | |
continue | |
diff = 0 | |
while True: | |
try: | |
data, addr = sock.recvfrom(1024) | |
except socket.error: | |
break | |
resp_time = datetime.now() | |
diff = (resp_time - sent_time).total_seconds() | |
print(resp_time.strftime("%H:%M:%S"), "Response from", | |
addr[0], "after {:.1f}ms".format(diff * 1000)) | |
time.sleep(wait - diff) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment