http server returns html source code on ports other than 80

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
masoldier
Posts: 4
Joined: Wed Aug 30, 2017 8:59 am

http server returns html source code on ports other than 80

Post by masoldier » Wed Aug 30, 2017 9:15 am

Hi there, I have been working on this simple http server on my esp8266 using micropython. The content that I want to server is HTML, a heading and a few buttons to control few GPIO pins on the board. But, whenever I change the port number to listen for any request, other than port 80, the web browser says "192.168.1.10 sent an invalid response. ERR_INVALID_HTTP_RESPONSE" (in chromium) and (in firefox) it returns the entire html code. I do not understand why this is happening? I tried changing the port with other python scripts which do not contain any HTML content and it works flawlessly. Please help me out here.

micropython Code:

import socket
import machine


#HTML to send to browsers
html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 LED ON/OFF</title> </head>
<center><h2>A simple webserver for turning HUZZAH Feather LED's on and off with Micropython</h2></center>
<center><h3>(for noobs to both the ESP8266 and Micropython)</h3></center>
<form>
LED0:
<button name="LED" value="ON0" type="submit">LED ON</button>
<button name="LED" value="OFF0" type="submit">LED OFF</button><br><br>
LED2:
<button name="LED" value="ON2" type="submit">LED ON</button>
<button name="LED" value="OFF2" type="submit">LED OFF</button>
</form>
</html>
"""

#Setup PINS
LED0 = machine.Pin(0, machine.Pin.OUT)
LED2 = machine.Pin(2, machine.Pin.OUT)

#Setup Socket WebServer
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 8080))
s.listen(5)
while True:
conn, addr = s.accept()
print("Got a connection from %s" % str(addr))
request = conn.recv(1024)
print("Content = %s" % str(request))
request = str(request)
LEDON0 = request.find('/?LED=ON0')
LEDOFF0 = request.find('/?LED=OFF0')
LEDON2 = request.find('/?LED=ON2')
LEDOFF2 = request.find('/?LED=OFF2')
#print("Data: " + str(LEDON0))
#print("Data2: " + str(LEDOFF0))
if LEDON0 == 6:
print('TURN LED0 ON')
LED0.off()
if LEDOFF0 == 6:
print('TURN LED0 OFF')
LED0.on()
if LEDON2 == 6:
print('TURN LED2 ON')
LED2.off()
if LEDOFF2 == 6:
print('TURN LED2 OFF')
LED2.on()
response = html
conn.send(response)
conn.close()

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: http server returns html source code on ports other than 80

Post by SpotlightKid » Thu Aug 31, 2017 5:29 am

You need to send a proper HTTP status line and response headers before the actual (HTML) response body.

masoldier
Posts: 4
Joined: Wed Aug 30, 2017 8:59 am

Re: http server returns html source code on ports other than 80

Post by masoldier » Thu Aug 31, 2017 5:45 am

Okay.. but how do I do that.. I am a newbie to programming and the server part is a little tricky for me.. can u please tell me what should I send as a response. What code should I write?

SpotlightKid
Posts: 463
Joined: Wed Apr 08, 2015 5:19 am

Re: http server returns html source code on ports other than 80

Post by SpotlightKid » Thu Aug 31, 2017 10:54 am

You just send the HTTP status line followed by CRLF, then the headers, each followed by CRLF, then another CRLF to end the headers and then the response body.

https://duckduckgo.com/lite?q=http+tutorial

masoldier
Posts: 4
Joined: Wed Aug 30, 2017 8:59 am

Re: http server returns html source code on ports other than 80

Post by masoldier » Tue Sep 05, 2017 7:09 am

Thank you so much, these tutorials are of true help. Appreciate it! Will get back if something comes up

Post Reply