Konubinix' opinionated web of thoughts

Simple Python Web Server in Android

Fleeting

Following the code of http.server and using a python runtime on android.

import contextlib

from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler, test
import plyer

class DualStackServer(ThreadingHTTPServer):

        def server_bind(self):
            # suppress exception when protocol is IPv4
            with contextlib.suppress(Exception):
                self.socket.setsockopt(
                    socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0)
            return super().server_bind()

        def finish_request(self, request, client_address):
            self.RequestHandlerClass(request, client_address, self,
                                     directory="/sdcard/localhost")

def run():
        plyer.vibrator.vibrate()
        test(port=9904, ServerClass=DualStackServer, HandlerClass=SimpleHTTPRequestHandler)

Notes linking here