forked from Tanganelli/CoAPthon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoapclient.py
143 lines (129 loc) · 4.09 KB
/
coapclient.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/bin/python
import getopt
import sys
from coapthon2.client.coap_protocol import HelperClient
client = HelperClient(server=("::1", 5683))
def usage():
print "Command:\tcoapclient.py -o[-p[-P]]"
print "Options:"
print "\t-o, --operation=\tGET|PUT|POST|DELETE|DISCOVER|OBSERVE"
print "\t-p, --path=\t\t\tPath of the request"
print "\t-P, --payload=\t\tPayload of the request"
def client_callback(response):
print "Callback"
def client_callback_observe(response):
print "Callback_observe"
check = True
while check:
chosen = raw_input("Stop observing? [y/N]: ")
if chosen != "" and not (chosen == "n" or chosen == "N" or chosen == "y" or chosen == "Y"):
print "Unrecognized choose."
continue
elif chosen == "y" or chosen == "Y":
while True:
rst = raw_input("Send RST message? [Y/n]: ")
if rst != "" and not (rst == "n" or rst == "N" or rst == "y" or rst == "Y"):
print "Unrecognized choose."
continue
elif rst == "" or rst == "y" or rst == "Y":
client.protocol.cancel_observing(response, True)
else:
client.protocol.cancel_observing(response, False)
check = False
break
else:
break
def main():
op = None
path = None
payload = None
try:
opts, args = getopt.getopt(sys.argv[1:], "ho:p:P:", ["help", "operation=", "path=", "payload="])
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage()
sys.exit(2)
for o, a in opts:
if o in ("-o", "--operation"):
op = a
elif o in ("-p", "--path"):
path = a
elif o in ("-P", "--payload"):
payload = a
elif o in ("-h", "--help"):
usage()
sys.exit()
else:
usage()
sys.exit(2)
if op is None:
print "Operation must be specified"
usage()
sys.exit(2)
if op == "GET":
if path is None:
print "Path cannot be empty for a GET request"
usage()
sys.exit(2)
function = client.protocol.get
args = (path,)
kwargs = {}
callback = client_callback
elif op == "OBSERVE":
if path is None:
print "Path cannot be empty for a GET request"
usage()
sys.exit(2)
function = client.protocol.observe
args = (path,)
kwargs = {}
callback = client_callback_observe
elif op == "DELETE":
if path is None:
print "Path cannot be empty for a DELETE request"
usage()
sys.exit(2)
function = client.protocol.delete
args = (path,)
kwargs = {}
callback = client_callback
elif op == "POST":
if path is None:
print "Path cannot be empty for a POST request"
usage()
sys.exit(2)
if payload is None:
print "Payload cannot be empty for a POST request"
usage()
sys.exit(2)
function = client.protocol.post
args = (path, payload)
kwargs = {}
callback = client_callback
elif op == "PUT":
if path is None:
print "Path cannot be empty for a PUT request"
usage()
sys.exit(2)
if payload is None:
print "Payload cannot be empty for a PUT request"
usage()
sys.exit(2)
function = client.protocol.put
args = (path, payload)
kwargs = {}
callback = client_callback
elif op == "DISCOVER":
function = client.protocol.discover
args = ()
kwargs = {}
callback = client_callback
else:
print "Operation not recognized"
usage()
sys.exit(2)
operations = [(function, args, kwargs, callback)]
client.start(operations)
if __name__ == '__main__':
main()