Webserver looses character

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
huvi
Posts: 2
Joined: Thu Nov 10, 2016 3:51 pm

Webserver looses character

Post by huvi » Thu Nov 10, 2016 4:40 pm

Hi!
I tried to implement a webserver that serves files from the filesystem. (html, css, js)
If the files are bigger then 10k, character are lost and the browser receives incomplete data.
For example i sended a file with 46.279 Bytes and the browser only receives 38.747 Bytes. I did a compare with "Beyond Compare" and it is not just the end of the file that is missing, there are holes.

I am using a NodeMCU board and the esp8266-20161017-v1.8.5.bin micropython image.

To verify my code, I tried other webserver and run into the same problem.
Does anyone have any idea why that happens?

This is the method that sends the file back to the browser:

def sendfile(socket, filename):
....print("Filename: {0}".format(filename))
....header = '''HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n'''
....socket.send(header)
....with open(filename, 'rb') as f:
........while True:
.............buffer = f.read(256)
.............if not buffer:
................break
.............print("Send buffer")
.............socket.send(buffer)

jms
Posts: 108
Joined: Thu May 05, 2016 8:29 pm
Contact:

Re: Webserver looses character

Post by jms » Fri Nov 11, 2016 8:09 am

Why not test the return value of send which is supposed to be the actual number of bytes sent ? Well I assume that's the case. The MicroPython docs don't say as much.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Webserver looses character

Post by Roberthh » Fri Nov 11, 2016 2:32 pm

Try socket.sendall(buffer)

huvi
Posts: 2
Joined: Thu Nov 10, 2016 3:51 pm

Re: Webserver looses character

Post by huvi » Fri Nov 11, 2016 3:09 pm

Yes, socket.sendall(buffer) solved the problem.
Ah, "socket.send()" stands for "send as much you want." ;-)

Thanks!

User avatar
ernitron
Posts: 89
Joined: Fri Jun 03, 2016 5:53 pm
Location: The Netherlands

Re: Webserver looses character

Post by ernitron » Fri Nov 11, 2016 6:41 pm

Roberthh wrote:Try socket.sendall(buffer)
That's what I use. I favor that. Should not loose a single byte...

Post Reply