def main(): global client host, port, path = parse_uri("coap://[::1]:5683/lights") try: tmp = socket.gethostbyname(host) host = tmp print "Host:", tmp except socket.gaierror: print("Exception") pass msg = Message() # msg.source = (host, port) # msg.destination = ('192.168.103.137', 5683) # msg.type = defines.Types['CON'] msg.payload = '{"message": "Hello"}' # msg.code = 2 # print msg.pretty_print() try: client = HelperClient(server=(host, port)) response = client.get(path) print response.pretty_print() response = client.post(path, msg.payload, timeout=10) print response.pretty_print() response = client.delete(path) print response.pretty_print() client.close() # client = CoAPclient(host, port, msg) except Exception as e: print "Exception: ", str(e) client.close()
def coap_encode(payload): message = Message() message.type = defines.Types['CON'] message.token = 4321 message.mid = 2 message.options = None message.payload = str(payload) serializer = Serializer() messagestring = serializer.serialize(message) return messagestring
def to_coap_response(http_response, request_method, client_address, mid): coap_msg = Message() coap_msg.destination = client_address coap_msg.type = Types["ACK"] coap_msg.code = CoAP_HTTP.to_coap_code(http_response.status_code, request_method) coap_msg.mid = mid if 'Content-Type' in http_response.headers: coap_msg.content_type = CoAP_HTTP.to_coap_content_type(http_response.headers['Content-Type'].split(";")[0]) else: coap_msg.content_type = 0 coap_msg.payload = http_response.content return coap_msg
def to_coap_response(http_response, request_method, client_address, mid): coap_msg = Message() coap_msg.destination = client_address coap_msg.type = Types["ACK"] coap_msg.code = CoAP_HTTP.to_coap_code(http_response.status_code, request_method) coap_msg.mid = mid if 'Content-Type' in http_response.headers: coap_msg.content_type = CoAP_HTTP.to_coap_content_type( http_response.headers['Content-Type'].split(";")[0]) else: coap_msg.content_type = 0 coap_msg.payload = http_response.content return coap_msg