-
Notifications
You must be signed in to change notification settings - Fork 198
/
handshake_server.py
48 lines (40 loc) · 1.3 KB
/
handshake_server.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
48
import logging
from ROAR.utilities_module.utilities import get_ip
import qrcode
import cv2
import numpy as np
import socket
from ROAR.utilities_module.utilities import get_ip
import socket
def showIPUntilAck():
img = np.array(qrcode.make(f"{get_ip()}").convert('RGB'))
success = False
addr = None
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
s.bind((get_ip(), 8008))
s.settimeout(1)
while True:
try:
s.listen()
cv2.imshow("Scan this code to connect to phone", img)
k = cv2.waitKey(1) & 0xff
if k == ord('q') or k == 27:
s.close()
break
conn, addr = s.accept()
addr = addr[0]
if conn:
s.close()
success = True
break
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
print(showIPUntilAck())