jczic / MicroWebSrv

A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
https://github.com/jczic/MicroWebSrv
MIT License
641 stars 116 forks source link

can't convert 'bytearray' object to str implicitly #4

Closed rdagger closed 6 years ago

rdagger commented 6 years ago

I am having trouble running the example with regard to browsing index.html on an ESP32. At first the index.html file was not found so I specified webPath='/www/' This fixed the file not found error.

But then I received a NameException: name _'tryAllocByteArray' is not defined in the ResponseWriteFile() method. I fixed the error by moving the function into the Response class.

Next I received the Exception: can't convert 'bytearray' object to str implicitly in the same ResponseWriteFile() method when it called the write() method. Looks like _client.socket.send(data) expects a string and not a byte array. Normally I would convert with decode('utf-8') but it does not appear to be implemented. Therefore I did an ugly hack of the write() method:

if type(data) == str:
                return self._client._socket.send(data)
            else:
                for b in data:
                    self._client._socket.send(chr(b))

The page is now loading as HTML with CSS formatting. However, the image is not loading. My write() method hack is throwing an [Errno 104] ECONNRESET. I'm don't know why.

jczic commented 6 years ago

Ok for "/www/" directly. Just, on pycom modules, all files are in '/flash/...'.

I made an error today in the call of tryAllocByteArray from _response class without test... I've fixed that. I've moved all utils function to a "static" logic in the class. Same for the public HTMLEscape() func. Now, call it like MicroWebSrv.HTMLEscape(...) directly.

The socket.send problem to using buffer protocol is not present on pycom modules, but I've changed that to socket.write. Could you try that on your esp32 (data can be str, bytes, bytearray, memoryview, ...) and tell me if that's works ?

2017-09-05 1:28 GMT+02:00 rdagger notifications@github.com:

I am having trouble running the example with regard to browsing index.html on an ESP32. At first the index.html file was not found so I specified webPath='/www/' This fixed the file not found error.

But then I received a NameException: name _'tryAllocByteArray' is not defined in the ResponseWriteFile() method. I fixed the error by moving the function into the Response class.

Next I received the Exception: can't convert 'bytearray' object to str implicitly in the same ResponseWriteFile() method when it called the write() method. Looks like _client.socket.send(data) expects a string and not a byte array. Normally I would convert with decode('utf-8') but it does not appear to be implemented. Therefore I did an ugly hack of the write() method:

if type(data) == str: return self._client._socket.send(data) else: for b in data: self._client._socket.send(chr(b))

The page is now loading as HTML with CSS formatting. However, the image is not loading. My write() method hack is throwing an [Errno 104] ECONNRESET. I'm don't know why.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv/issues/4, or mute the thread https://github.com/notifications/unsubscribe-auth/AAegLD_48fWiJIEYYMtwGy1hqaCXxonKks5sfIeugaJpZM4PMTMY .

--

Sincèrement,

Jean-Christophe Bos jczic.bos@gmail.com jczic.bos@gmail.com

rdagger commented 6 years ago

Works great! Runs fast too. I totally appreciate your help. Thanks.

jczic commented 6 years ago

Yeah :) I totally appreciate yours feedback too !

2017-09-05 5:05 GMT+02:00 rdagger notifications@github.com:

Closed #4 https://github.com/jczic/MicroWebSrv/issues/4.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/jczic/MicroWebSrv/issues/4#event-1234117430, or mute the thread https://github.com/notifications/unsubscribe-auth/AAegLAq1Ea8JZL5wPVD2WlX34v04RW79ks5sfLpigaJpZM4PMTMY .

--

Sincèrement,

Jean-Christophe Bos jczic.bos@gmail.com jczic.bos@gmail.com