def test_encode(self): msg1 = coap.Message(mtype=coap.CON, mid=0) binary1 = chr(64)+chr(0)+chr(0)+chr(0) self.assertEqual(msg1.encode(), binary1, "wrong encode operation for empty CON message") msg2 = coap.Message(mtype=coap.ACK, mid=0xBC90, code=coap.CONTENT, payload="temp = 22.5 C", token='q') msg2.opt.etag = "abcd" binary2 = chr(97)+chr(69)+chr(188)+chr(144)+chr(113)+chr(68)+"abcd"+chr(255)+"temp = 22.5 C" self.assertEqual(msg2.encode(), binary2, "wrong encode operation for ACK message with payload, and Etag option") msg3 = coap.Message() self.assertRaises(TypeError, msg3.encode)
def putResource(self): payload = "Poland CAN into space!!! Poland MUST into space!!! Poland WILL into space!!!!" request = coap.Message(code=coap.PUT, payload=payload) request.opt.uri_path = ("other", "block") request.remote = ('127.0.0.1', coap.COAP_PORT) d = protocol.request(request) d.addCallback(self.printResponse)
def render_GET(self, request): data = [] self.root.generateResourceList(data, "") payload = ",".join(data) print payload response = coap.Message(code=coap.CONTENT, payload=payload) response.opt.content_format = 40 return defer.succeed(response)
def requestResource(self): request = coap.Message(code=coap.POST, payload="-252.6") #request.opt.uri_path = ('other', 'separate') request.opt.uri_path = ('/1a/temp', ) request.opt.uri_query = ('98acd7f4dc8dd67cee40462ea623f99c8bc4adf1', ) request.remote = ("65.49.60.152", coap.COAP_PORT) #request.setObserve(self.printLaterResponse) d = protocol.request(request)
def prepare_request(self, *args): card = ResponseCard(controller=self) self.grid.add_widget(card) log.msg("send request %s" % self.active_method) request = coap.Message() request.code = coap.requests_rev[self.active_method] card.target_uri.text = self.active_method + ' ' + self.address_bar.text card.request_uri = self.address_bar.text if request.code is coap.GET: accept = self.method_chooser.accept_field.text if accept in coap.media_types_rev: request.opt.accept = coap.media_types_rev[accept] observe = self.method_chooser.observe_field.active if observe is True: request.setObserve(self.process_update, card) if request.code is coap.PUT: payload = self.method_chooser.put_payload_box.text payload = payload.encode('utf-8') request.payload = payload content_format = self.method_chooser.content_format_put_field.text if content_format in coap.media_types_rev: request.opt.content_format = coap.media_types_rev[ content_format] if request.code is coap.POST: payload = self.method_chooser.post_payload_box.text payload = payload.encode('utf-8') request.payload = payload content_format = self.method_chooser.content_format_post_field.text if content_format in coap.media_types_rev: request.opt.content_format = coap.media_types_rev[ content_format] d = parseURI(self.address_bar.text).addErrback( self.handle_DNS_failure, card).addCallback(self.send_request, card, request) d.addCallback(self.process_response, card) d.addErrback(self.print_error, card) card.deferred = d
def responseReady(self, d, request): print 'response ready. sending...' payload = "Three rings for the elven kings under the sky, seven rings for dwarven lords in their halls of stone, nine rings for mortal men doomed to die, one ring for the dark lord on his dark throne." response = coap.Message(code=coap.CONTENT, payload=payload) d.callback(response)
def render_PUT(self, request): print 'PUT payload: ' + request.payload payload = "Mr. and Mrs. Dursley of number four, Privet Drive, were proud to say that they were perfectly normal, thank you very much." response = coap.Message(code=coap.CHANGED, payload=payload) return defer.succeed(response)
def render_GET(self, request): payload=" Now I lay me down to sleep, I pray the Lord my soul to keep, If I shall die before I wake, I pray the Lord my soul to take." response = coap.Message(code=coap.CONTENT, payload=payload) return defer.succeed(response)
def render_GET(self, request): response = coap.Message(code=coap.CONTENT, payload='%d' % (self.counter,)) self.counter += 1 return defer.succeed(response)
def render_GET(self, request): response = coap.Message(code=coap.CONTENT, payload=datetime.datetime.now().strftime("%Y-%m-%d %H:%M")) return defer.succeed(response)