Skip to content

as such it is a dedicated server #15

Open
@WimWWimW

Description

@WimWWimW

The while-True loop in start() blocks any other activities on the controler, such as watching for pressed buttons. With a small modification of your code this would be solved without resorting to multithreading. I implemented it as subclass, but it could easily be integrated:

`import socket

from micropyserver import MicroPyServer

class WebServer(MicroPyServer):

def __init__(self, host="0.0.0.0", port=80, blocking = True):
    super().__init__(host, port)
    self.blocking = blocking
    

def start(self):
    """ Start server """
    self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self._sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    self._sock.bind((self._host, self._port))
    self._sock.listen(1)
    print("Server start")
    if self.blocking:
        while True:
            self.poll()
        
        
def poll(self): 
    if self._sock is not None:
        try:
            self._connect, address = self._sock.accept()
            request = self.get_request()
            if len(request) == 0:
                self._connect.close()
                return
            
            if self._on_request_handler:
                if not self._on_request_handler(request, address):
                    return
                
            route = self.find_route(request)
            if route:
                route["handler"](self, request)
            else:
                self._route_not_found(request)
                
        except Exception as e:
            self._internal_error(e)
        finally:
            self._connect.close()

`

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions