mwasilak / txThings

CoAP library for Twisted
94 stars 43 forks source link

Resource not found! #25

Closed Ruritsuki closed 5 years ago

Ruritsuki commented 5 years ago

Hi why do I run server.py with python3 and then run clientGet.py to get these?

2018-12-15 10:06:40+0800 [-] Message b'D\x01\x07\xf6\x00\x00\xc5\xfa`Ttest' sent successfully 2018-12-15 10:06:40+0800 [-] Sending request - Token: b'0000c5fa', Host: 192.168.90.86, Port: 5683 2018-12-15 10:06:40+0800 [Coap (UDP)] Received b'd\x84\x07\xf6\x00\x00\xc5\xfa\xffError: Resource not found!' from 192.168.90.86:5683 2018-12-15 10:06:40+0800 [Coap (UDP)] Incoming Message ID: 2038 2018-12-15 10:06:40+0800 [Coap (UDP)] New unique ACK or RST message received 2018-12-15 10:06:40+0800 [Coap (UDP)] Exchange removed, Message ID: 2038. 2018-12-15 10:06:40+0800 [Coap (UDP)] Received Response, token: b'0000c5fa', host: 192.168.90.86, port: 5683 2018-12-15 10:06:40+0800 [-] First result: Error: Resource not found!

What should I do? thank you very much!!

engrjislam commented 5 years ago

I am getting same error. Any help?

Ruritsuki commented 5 years ago

I think it's a problem with the python version. I tried running python2.7 with server.py and running clientGet.py with python3, then there is no error.

engrjislam commented 1 year ago

This issue is all about the encoding and decoding mechanism of Python3. I had to change in -- (1) clientGET.py, (2) coap.py and (3) resource.py

    def requestResource(self):
        request = coap.Message(code=coap.GET)
        # Send request to "coap://coap.me:5683/time"
        request.opt.uri_path = (b'time',)
        request.opt.observe = 0
        # ---------------------------- added by johirul ---------------------------- #
        if sys.version_info.major == 2:
            # -- python 2
            request.remote = (ip_address(unicode("127.0.0.1")), coap.COAP_PORT) 
        else:
            request.remote = (ip_address("127.0.0.1"), coap.COAP_PORT)
        # ----------------------------x----------------x---------------------------- #

        d = protocol.request(request, observeCallback=self.printLaterResponse)
        d.addCallback(self.printResponse)
        d.addErrback(self.noResponse)

    def printResponse(self, response):
        # ---------------------------- added by johirul ---------------------------- #
        if sys.version_info.major > 2:
            payload = response.payload.decode()
        else:
            payload = response.payload
        # ----------------------------x----------------x---------------------------- #
        print('First result: ' + payload)
        # reactor.stop()

    def printLaterResponse(self, response):
        # ---------------------------- added by johirul ---------------------------- #
        if sys.version_info.major > 2:
            payload = response.payload.decode()
        else:
            payload = response.payload
        # ----------------------------x----------------x---------------------------- #    
        print('Observe result: ' + payload)
    def encode(self):
        """Create binary representation of message from Message object."""
        if self.mtype is None or self.mid is None:
            raise TypeError("Fatal Error: Message Type and Message ID must not be None.")
        rawdata = six.int2byte((self.version << 6) + ((self.mtype & 0x03) << 4) + (len(self.token) & 0x0F))
        rawdata += struct.pack('!BH', self.code, self.mid)
        rawdata += self.token
        rawdata += self.opt.encode()
        if len(self.payload) > 0:
            rawdata += six.int2byte(0xFF)
            # ---------------------------- added by johirul ---------------------------- #
            if sys.version_info.major == 2:
                # -- python 2
                rawdata += self.payload 
            else:
                rawdata += self.payload.encode()
            # ----------------------------x----------------x---------------------------- #
        return rawdata

    --- --- --- --- --- 

    def datagramReceived(self, data, remote):
        host, port = remote
        log.msg("Received %r from %s:%d" % (data, host, port))
        # ---------------------------- added by johirul ---------------------------- #
        if sys.version_info.major == 2:
            # -- python 2
            message = Message.decode(data, (ip_address(unicode(host)), port), self) 
        else:
            message = Message.decode(data, (ip_address(host), port), self)
        # ----------------------------x----------------x---------------------------- #
        if self.deduplicateMessage(message) is True:
            return
        if isRequest(message.code):
            self.processRequest(message)
        elif isResponse(message.code):
            self.processResponse(message)
        elif message.code is EMPTY:
            self.processEmpty(message)
    def getChildWithDefault(self, path, request):
        """
        Retrieve a static or dynamically generated child resource from me.

        First checks if a resource was added manually by putChild, and then
        call getChild to check for dynamic resources. Only override if you want
        to affect behaviour of all child lookups, rather than just dynamic
        ones.

        This will check to see if I have a pre-registered child resource of the
        given name, and call getChild if I do not.
        """
        # ---------------------------- added by johirul ---------------------------- #
        if sys.version_info.major > 2:
            path = path.decode()
        # ----------------------------x----------------x---------------------------- #
        if path in self.children:
            return self.children[path]
        return self.getChild(path, request)

These changes enable me to run txThings both with python2 (2.7.18) and python3 (3.10.6).