Simple HTTP framework
Simple HTTP framework
I'm sure a million of these have been written already, but in the spirit of the recent FTP and Telnet servers, I have written a simple HTTP server and framework for micro python and the ESP8266. I feel like it's 1992 all over again!
https://github.com/fadushin/esp8266/tre ... hon/uhttpd
I am not a professional python coder by any stretch of the imagination, so any comments, criticisms, PRs, etc are appreciated. I wrote this as a framework for writing a web interface to control a simple ESP8266 application I am blogging about, but I thought it might be of more use, generally to people. The accepting socket is meant to run in the background, but to keep things simple, I have left the sockets representing the client connections in blocking mode.
Also, in case it is not glaringly obvious already, this software provides NO SECURITY and may expose sensitive security information, such as webrepl or other passwords, to malicious agents on your network. This software is NOT intended for production use!
Enjoy!
https://github.com/fadushin/esp8266/tre ... hon/uhttpd
I am not a professional python coder by any stretch of the imagination, so any comments, criticisms, PRs, etc are appreciated. I wrote this as a framework for writing a web interface to control a simple ESP8266 application I am blogging about, but I thought it might be of more use, generally to people. The accepting socket is meant to run in the background, but to keep things simple, I have left the sockets representing the client connections in blocking mode.
Also, in case it is not glaringly obvious already, this software provides NO SECURITY and may expose sensitive security information, such as webrepl or other passwords, to malicious agents on your network. This software is NOT intended for production use!
Enjoy!
Last edited by fdushin on Mon Jan 02, 2017 12:51 am, edited 1 time in total.
Re: Simple HTTP framework
I've posted some improvements. While the server does not (yet) support SSL, some of the caveats are now mitigated by using a root path ('/www', but default) to service files. The server now supports a REST-based JSON API. Eventually I'd like to write a thick web client using backbone.js or equivalent for managing the device (networking, time, memory usage, etc) through a web console.
Re: Simple HTTP framework
Thanks for posting your work!
I've given it a spin and noticed that I could only get it to work from a hard reset. When I try to start it from a soft reboot, I get this... Here is my simple test (test_http.py)...
and (main.py)...
Is this a memory issue?
I've given it a spin and noticed that I could only get it to work from a hard reset. When I try to start it from a soft reboot, I get this... Here is my simple test (test_http.py)...
Code: Select all
import uhttpd
import http_file_handler
def main():
server = uhttpd.Server([('/', http_file_handler.Handler())])
server.start()
Code: Select all
import test_http
test_http.main()
Re: Simple HTTP framework
I get that all the time during testing. That happens when you leave your code with open connections. If you close all sockets, then soft reboot works fine.
Re: Simple HTTP framework
Thanks @Roberthh. I almost never use soft reboots, so I had never seen this.Roberthh wrote:I get that all the time during testing. That happens when you leave your code with open connections. If you close all sockets, then soft reboot works fine.
@bitninja You can stop the server by using the stop() method, and that seems to do the trick.
Re: Simple HTTP framework
Thanks for the info. How do I close open sockets ? (When they are embedded in a class). Are there some generic commands for closing any open sockets?Roberthh wrote:I get that all the time during testing. That happens when you leave your code with open connections. If you close all sockets, then soft reboot works fine.
I've changed my test script...
Code: Select all
import uhttpd
import http_file_handler
def main():
server = uhttpd.Server([('/', http_file_handler.Handler())])
server.start()
server.stop()
Re: Simple HTTP framework
That's hard to tell and depends on you set-up. I assume that the server.stop() method closes all sockets opened by the server class. Butthere may be more open, like when you connect through webrepl.How do I close open sockets ?
For myself, I do not care so much, since during testing a) pushing the reset button is more convenient than typing stop() + Ctrl-D, and b) reset bring the device into a well defined initial state. And when a system with a server is done and operational, it's not supposed to stop.
Re: Simple HTTP framework
When you start the uhttpd server, that will open a server-side socket which is listening on port 80 (by default) for connections from clients. That socket is needed for the server to service HTTP requests, and will stay open until the server is stopped.Thanks for the info. How do I close open sockets ? (When they are embedded in a class). Are there some generic commands for closing any open sockets?
This will just start the server and then stop it. That's probably not what you want, if you want to use the server.I've changed my test script...Code: Select all
import uhttpd import http_file_handler def main(): server = uhttpd.Server([('/', http_file_handler.Handler())]) server.start() server.stop()
I think what you want is a command that will do a soft reset, but stop the server first. I am not sure why a soft reset is necessary for you -- I usually just use machine.reset(). So you could try:But I am still unclear as to how I am getting into a state with an open socket. Hows does one do that?
Code: Select all
import uhttpd
import http_file_handler
def main():
server = uhttpd.Server([('/', http_file_handler.Handler())])
server.start()
def reboot():
server.stop()
import machine
machine.reset()
Re: Simple HTTP framework
OK, I think I've got it. Thanks everybody for the information and the help.
Re: Simple HTTP framework
I moved the uhttpd module to https://github.com/fadushin/esp8266/tre ... hon/uhttpd and added support for HTTP Basic authentication.
In manipulating HTML and Javascript files on the ESP8266, I found myself wanting a shell, so I wrote a very basic one called ush, which you can find at
https://github.com/fadushin/esp8266/tre ... thon/tools
It, too, takes memory to run, so I'd recommend burning it into flash, but it's really more a of a development tool, rather than a runtime production tool.
Enjoy!
In manipulating HTML and Javascript files on the ESP8266, I found myself wanting a shell, so I wrote a very basic one called ush, which you can find at
https://github.com/fadushin/esp8266/tre ... thon/tools
It, too, takes memory to run, so I'd recommend burning it into flash, but it's really more a of a development tool, rather than a runtime production tool.
Enjoy!