#!/usr/bin/env micropython ## ## The Ridiculously Simple MicroPython Nex Server ## ## trsmpns.upy.py ## \ \ ## \ editors shall read this as Python source ## but MicroPython != Python ## e.g. sockets are streams by default ## import os, socket def main(): s = socket.socket() ai = socket.getaddrinfo("0.0.0.0", 1900) addr = ai[0][-1] s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(addr) s.listen(5) while True: client_stream, client_addr = s.accept() req = client_stream.readline()[1:-1].decode() # "/req\n" -> "req" while True: ## try and ignore errors. try: client_stream.write(open(req).read()) break ## req was a file except: pass try: client_stream.write(open("./"+req+"/index").read()) break ## req was a dir with index file except: pass try: ## silently ignore other types than file and dir client_stream.write("=> ..\n") for f in os.listdir("./"+req): st = os.stat("./"+req+"/"+f)[0] & 0xc000 if st == 0x8000: tc = "" elif st == 0x4000: tc = "/" if st: client_stream.write("=> "+f+tc+"\n") break ## req was a dir without index file except: pass break client_stream.close() main()