-
Notifications
You must be signed in to change notification settings - Fork 0
/
handshake.py
47 lines (40 loc) · 1.42 KB
/
handshake.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import logging
from ROAR.utilities_module.utilities import get_ip
import qrcode
import cv2
import numpy as np
import socket
def showIPUntilAckUDP():
img = np.array(qrcode.make(f"{get_ip()}").convert('RGB'))
success = False
addr = None
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(0.1)
try:
s.bind((get_ip(), 8008))
while success is False:
try:
cv2.imshow("Scan this code to connect to phone", img)
k = cv2.waitKey(1) & 0xff
seg, addr = s.recvfrom(1024) # this command might timeout
if k == ord('q') or k == 27:
s.close()
break
addr = addr
success = True
for i in range(10):
print("data sent")
s.sendto(b"hi", addr)
except socket.timeout as e:
logging.info(f"Please tap on the ip address to scan QR code. ({get_ip()}:{8008}). {e}")
except Exception as e:
logging.error(f"Unable to bind socket: {e}")
finally:
s.close()
cv2.destroyWindow("Scan this code to connect to phone")
return success, addr[0]
logging.basicConfig(format='%(levelname)s - %(asctime)s - %(name)s '
'- %(message)s',
datefmt="%H:%M:%S",
level=logging.DEBUG)
print(showIPUntilAckUDP())