This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.py
61 lines (56 loc) · 1.75 KB
/
client.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
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Basic client to query in Python a raspberry to communicate its neighbor
networks.
Usage:
python -m client [HOST]
Connects to the HOST and performs all available client operations several times
"""
# Libraries
import sys
from socket import *
import netifaces as ni
import struct
from common import PORT, OP_WIFI_REQ, OP_CLOSE
# Constants
HOST = "192.168.2.200" if len(sys.argv) <= 1 else sys.argv[1]
"""
str: ip of the Raspberry server to connect
"""
TESTS_NUMBER = 1 if len(sys.argv) <= 2 else int(sys.argv[2])
"""
int: number of tests to perform
"""
# Welcome
print("Welcome to Raspberry WiFi notifier for drones [CLIENT]")
print("IP Address of the raspberry server to query: %s" % (HOST))
# Set client
for i in range(TESTS_NUMBER):
if(TESTS_NUMBER > 1):
print("CONNECTION TEST [%02d]" % (i, HOST))
print("================================================")
print(" 01. Creating socket")
s = socket(AF_INET, SOCK_STREAM)
print(" 02. Connecting socket")
s.connect((HOST, PORT))
# Socket connected
print(" 03. Connected to socket")
print(" 04. Testing features")
print(" -> Testing OP_WIFI_REQ")
# Ask for networks
s.send(OP_WIFI_REQ)
print(" --> Sent opcode")
print(" --> Waiting for size response")
size_response = s.recv(8)
size_json = struct.unpack("Q", size_response)[0]
print(" --> Response size is %d bytes" % size_json)
print(" --> Receiving networks in JSON format")
json = s.recv(size_json)
print(" --> Received JSON is")
print(json)
print(" --> Feature tested")
# Close connection
print(" -> Testing OP_CLOSE")
s.send(OP_CLOSE)
print(" --> Sent close opcode")
s.close()
print(" 05. Closed communications")