Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as such it is a dedicated server #15

Open
WimWWimW opened this issue Oct 26, 2024 · 1 comment
Open

as such it is a dedicated server #15

WimWWimW opened this issue Oct 26, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@WimWWimW
Copy link

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()

`

@eng1234
Copy link

eng1234 commented Nov 16, 2024

Nice

@troublegum troublegum added the enhancement New feature or request label Jan 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants