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()
http server returns html source code on ports other than 80
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: http server returns html source code on ports other than 80
You need to send a proper HTTP status line and response headers before the actual (HTML) response body.
Re: http server returns html source code on ports other than 80
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?
-
- Posts: 463
- Joined: Wed Apr 08, 2015 5:19 am
Re: http server returns html source code on ports other than 80
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
https://duckduckgo.com/lite?q=http+tutorial
Re: http server returns html source code on ports other than 80
Thank you so much, these tutorials are of true help. Appreciate it! Will get back if something comes up