Chat freely about anything...

User avatar
By Gregory
#88763 Hello everyone!
I want send JSON String (JavaScript) form WebPage to ESP8266 WebServer (Micropython). When I recived POST Request, see only part of message:

Connected by ('192.168.0.2', 57002)

b'POST /jsonData HTTP/1.1\r\nHost: 192.168.0.1\r\nConnection: keep-alive\r\nContent-Length: 143\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36\r\nContent-Type: application/json\r\nAccept: */*\r\nOrigin: http://192.168.0.1\r\nReferer: http://192.168.0.1/\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7\r\n\r\n{"LED_1__VERY_LONG_VARIABLE_NAME__VERY_LONG_VARIABLE_NAME__":true,"LED_2_VERY_LONG_VARIABLE_NAME__VERY_LONG_VARIABLE_NAME__":tr'


but I expected like this:

Connected by ('192.168.0.2', 57002)

b'POST /jsonData HTTP/1.1\r\nHost: 192.168.0.1\r\nConnection: keep-alive\r\nContent-Length: 143\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36\r\nContent-Type: application/json\r\nAccept: */*\r\nOrigin: http://192.168.0.1\r\nReferer: http://192.168.0.1/\r\nAccept-Encoding: gzip, deflate\r\nAccept-Language: pl-PL,pl;q=0.9,en-US;q=0.8,en;q=0.7\r\n\r\n{"LED_1__VERY_LONG_VARIABLE_NAME__VERY_LONG_VARIABLE_NAME__":true,"LED_2_VERY_LONG_VARIABLE_NAME__VERY_LONG_VARIABLE_NAME__":true,"LED_3":false}'


and here is a code:
Code: Select all        while True:
            try:
                self.conn, addr = self.s.accept()
            except Exception as exc:
                print("Socket Accept Error ", exc.args[0])
                reset()
                pass
            print('Connected by', addr)
            try:

                self.request = str(self.conn.recv(1024))  # <-- Read Buffer
                print(self.request) # <-- Print POST Request msg

            except Exception as exc:
                print("recv -------------", exc.args[0])
                reset()
                pass
            self.connection(html)


So, I have a problem with read all buffer using socket.recv(bufsize) function. The Data are truncated.

Read all socket buffer at once is not possible, so i decided read chunks - 8 Bytes each. Everything is OK when the last chunk have a other size (less than 8 bytes).

Sometimes the last chunk of data have a exactly the same length which previous chunks (8 bytes) and never break loop. So, how i can recognize last chunk and break infinite while loop?

Code:

Code: Select all        while True:
            try:
                self.conn, addr = self.s.accept()
            except Exception as exc:
                print("Socket Accept Error ", exc.args[0])
                reset()
                pass
            print('Connected by', addr)
            try:
               
                while True:
                    self.request = self.conn.recv(8) # <-- Read Chunk
                    print(str(self.request))
                    if len(self.request) < 8: # <-- Check length
                        print("End")
                        break                 
                    self.request = None

            except Exception as exc:
                print("recv -------------", exc.args[0])
                reset()
                pass
            self.connection(html)