[Swlug] Python captive hotspot

Rhys Sage rhys_sage at yahoo.com
Sun Jun 25 12:35:14 UTC 2023


I'm really not familiar with some of the concepts in this code. I'm not quite sure what does what and why. What I want to do with it is to modify the code such that the index page is dynamic. The goal is to include a stream from a camera on the index page and some buttons. That way nobody has to log onto the network or do anything funky in order just to use it. I'll be applying this to a Pi pico W. As is, it works on the Pico W. When I tried to put the webpage in the code rather than as an external file, it all fell apart. 

The goal is to control a toy car with a camera aboard for 1st person driving experience. Then having done that, I'd like to lock the button control down to specified mac addresses. That way everybody can see but only authorised people can drive. I just built the car kit this morning.


from phew import logging, server, access_point, dns
from phew.template import render_template
from phew.server import redirect
DOMAIN = "CIA.wireless"  # This is the address that is shown on the Captive Portal
@server.route("/", methods=['GET'])
def index(request):
    """ Render the Index page"""
    if request.method == 'GET':
        logging.debug("Get request")
        print("server route")
        return render_template("index.html")

# microsoft windows redirects
@server.route("/ncsi.txt", methods=["GET"])
def hotspot(request):
    print(request)
    print("ncsi.txt")
    return "", 200

@server.route("/connecttest.txt", methods=["GET"])
def hotspot(request):
    print(request)
    print("connecttest.txt")
    return "", 200

@server.route("/redirect", methods=["GET"])
def hotspot(request):
    print(request)
    print("****************ms redir*********************")
    return redirect(f"http://{DOMAIN}/", 302)

# android redirects
@server.route("/generate_204", methods=["GET"])
def hotspot(request):
    print(request)
    print("******generate_204********")
    return redirect(f"http://{DOMAIN}/", 302)

# apple redir
@server.route("/hotspot-detect.html", methods=["GET"])
def hotspot(request):
    print(request)
    """ Redirect to the Index Page """
    return render_template("index.html")

@server.catchall()
def catch_all(request):
    print("***************CATCHALL***********************\n" + str(request))
    return redirect("http://" + DOMAIN + "/")

# Set to Accesspoint mode
# Change this to whatever Wifi SSID you wish
ap = access_point("CIA Test Server")
ip = ap.ifconfig()[0]
# Grab the IP address and store it
logging.info(f"starting DNS server on {ip}")
# # Catch all requests and reroute them
dns.run_catchall(ip)
print("line 77")
server.run()                            # Run the server
logging.info("Webserver Started")



Rhys Sage



More information about the Swlug mailing list