added live stream webcam example to show how to work with socket.io-python

Commit c7cc08f · patx · 01/27/2025 at 4:53PM

Changeset
c7cc08f4088dbf0f60a33c047dc23d7de7e72076
Parents
fbcfb598034b742cc29c146e65811166eb57ef0d

View source at this commit

Comments

No comments yet.

Log in to comment

Diff

diff --git a/MicroPie.py b/MicroPie.py
index 1c11fe0..048adcd 100644
--- a/MicroPie.py
+++ b/MicroPie.py
@@ -178,7 +178,8 @@ class Server:
             self.path_params = path_parts
             handler_function = getattr(self, "index", None)
 
-        self.query_params = parse_qs(environ["QUERY_STRING"])
+        self.query_params = parse_qs(environ.get("QUERY_STRING", ""))
+
 
         class MockRequestHandler:
             def __init__(self, environ: Dict[str, Any]) -> None:
diff --git a/README.md b/README.md
index 273178a..5892228 100644
--- a/README.md
+++ b/README.md
@@ -90,7 +90,7 @@ You can also pass parameters directly in the URL path instead of query strings:
 
 ```python
 class MyApp(Server):
-    def greet(self, name):
+    def greet(self, name="Guest"):
         return f"Hello, {name}!"
 ```
 
diff --git a/examples/pastebin/MicroPie.py b/examples/pastebin/MicroPie.py
new file mode 100644
index 0000000..1c11fe0
--- /dev/null
+++ b/examples/pastebin/MicroPie.py
@@ -0,0 +1,385 @@
+"""
+MicroPie: A simple Python ultra-micro web framework with WSGI
+support. https://patx.github.io/micropie
+
+Copyright Harrison Erd
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+1. Redistributions of source code must retain the above copyright notice,
+   this list of conditions and the following disclaimer.
+
+2. Redistributions in binary form must reproduce the above copyright notice,
+   this list of conditions and the following disclaimer in the documentation
+   and/or other materials provided with the distribution.
+
+3. Neither the name of the copyright holder nor the names of its
+   contributors may be used to endorse or promote products derived from this
+   software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
+CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+"""
+
+from wsgiref.simple_server import make_server
+import time
+import uuid
+import inspect
+import os
+import mimetypes
+from urllib.parse import parse_qs
+from typing import Optional, Dict, Any, Union, Tuple, List
+
+try:
+    from jinja2 import Environment, FileSystemLoader
+    JINJA_INSTALLED = True
+except ImportError:
+    JINJA_INSTALLED = False
+
+
+class Server:
+    SESSION_TIMEOUT: int = 8 * 3600  # 8 hours
+
+    def __init__(self) -> None:
+        if JINJA_INSTALLED:
+            self.env = Environment(loader=FileSystemLoader("templates"))
+
+        self.sessions: Dict[str, Dict[str, Any]] = {}
+        self.query_params: Dict[str, List[str]] = {}
+        self.body_params: Dict[str, List[str]] = {}
+        self.path_params: List[str] = []
+        self.session: Dict[str, Any] = {}
+        self.environ: Optional[Dict[str, Any]] = None
+        self.start_response: Optional[Any] = None
+
+    def run(self, host: str = "127.0.0.1", port: int = 8080) -> None:
+        print(f"Serving on http://{host}:{port}")
+        with make_server(host, port, self.wsgi_app) as httpd:
+            try:
+                httpd.serve_forever()
+            except KeyboardInterrupt:
+                print("\nShutting down server...")
+
+    def get_session(self, request_handler: Any) -> Dict[str, Any]:
+        cookie = request_handler.headers.get("Cookie")
+        session_id = None
+
+        if cookie:
+            cookies = {
+                item.split("=")[0].strip(): item.split("=")[1].strip()
+                for item in cookie.split(";")
+            }
+            session_id = cookies.get("session_id")
+
+        if not session_id or session_id not in self.sessions:
+            session_id = str(uuid.uuid4())
+            self.sessions[session_id] = {"last_access": time.time()}
+            request_handler.send_response(200)
+            request_handler.send_header(
+                "Set-Cookie", f"session_id={session_id}; Path=/; HttpOnly; SameSite=Strict"
+            )
+            request_handler.end_headers()
+
+        session = self.sessions.get(session_id)
+        if session:
+            session["last_access"] = time.time()
+        else:
+            session = {"last_access": time.time()}
+            self.sessions[session_id] = session
+
+        return session
+
+    def cleanup_sessions(self) -> None:
+        now = time.time()
+        self.sessions = {
+            sid: data
+            for sid, data in self.sessions.items()
+            if data.get("last_access", now) + self.SESSION_TIMEOUT > now
+        }
+
+    def redirect(self, location: str) -> Tuple[int, str]:
+        return (
+            302,
+            (
+                "<html><head>"
+                f"<meta http-equiv='refresh' content='0;url={location}'>"
+                "</head></html>"
+            ),
+        )
+
+    def render_template(self, name: str, **kwargs: Any) -> str:
+        if not JINJA_INSTALLED:
+            raise ImportError("Jinja2 is not installed.")
+        return self.env.get_template(name).render(kwargs)
+
+    def serve_static(self, filepath: str) -> Union[Tuple[int, str], Tuple[int, bytes, List[Tuple[str, str]]]]:
+        safe_root = os.path.abspath("static")
+        requested_file = os.path.abspath(os.path.join("static", filepath))
+        if not requested_file.startswith(safe_root):
+            return 403, "403 Forbidden"
+        if not os.path.isfile(requested_file):
+            return 404, "404 Not Found"
+        content_type, _ = mimetypes.guess_type(requested_file)
+        if not content_type:
+            content_type = "application/octet-stream"
+        with open(requested_file, "rb") as f:
+            content = f.read()
+        return 200, content, [("Content-Type", content_type)]
+
+    def validate_request(self, method: str) -> bool:
+        try:
+            if method == "GET":
+                for key, value in self.query_params.items():
+                    if (
+                        not isinstance(key, str)
+                        or not all(isinstance(v, str) for v in value)
+                    ):
+                        print(f"Invalid query parameter: {key} -> {value}")
+                        return False
+
+            if method == "POST":
+                for key, value in self.body_params.items():
+                    if (
+                        not isinstance(key, str)
+                        or not all(isinstance(v, str) for v in value)
+                    ):
+                        print(f"Invalid body parameter: {key} -> {value}")
+                        return False
+
+            return True
+        except Exception as e:
+            print(f"Error during request validation: {e}")
+            return False
+
+    def wsgi_app(self, environ: Dict[str, Any], start_response: Any) -> List[bytes]:
+        self.environ = environ
+        self.start_response = start_response
+
+        path = environ["PATH_INFO"].strip("/")
+        method = environ["REQUEST_METHOD"]
+
+        path_parts = path.split("/") if path else []
+        func_name = path_parts[0] if path_parts else "index"
+        self.path_params = path_parts[1:] if len(path_parts) > 1 else []
+
+        handler_function = getattr(self, func_name, None)
+
+        if not handler_function:
+            self.path_params = path_parts
+            handler_function = getattr(self, "index", None)
+
+        self.query_params = parse_qs(environ["QUERY_STRING"])
+
+        class MockRequestHandler:
+            def __init__(self, environ: Dict[str, Any]) -> None:
+                self.environ = environ
+                self.headers = {
+                    key[5:].replace("_", "-").lower(): value
+                    for key, value in environ.items()
+                    if key.startswith("HTTP_")
+                }
+                self.cookies = self._parse_cookies()
+                self._headers_to_send: List[Tuple[str, str]] = []
+
+            def _parse_cookies(self) -> Dict[str, str]:
+                cookies = {}
+                if "HTTP_COOKIE" in self.environ:
+                    cookie_header = self.environ["HTTP_COOKIE"]
+                    for cookie in cookie_header.split(";"):
+                        if "=" in cookie:
+                            k, v = cookie.strip().split("=", 1)
+                            cookies[k] = v
+                return cookies
+
+            def send_response(self, code: int) -> None:
+                pass
+
+            def send_header(self, key: str, value: str) -> None:
+                self._headers_to_send.append((key, value))
+
+            def end_headers(self) -> None:
+                pass
+
+        request_handler = MockRequestHandler(environ)
+
+        session_id = request_handler.cookies.get("session_id")
+        if session_id and session_id in self.sessions:
+            self.session = self.sessions[session_id]
+            self.session["last_access"] = time.time()
+        else:
+            session_id = str(uuid.uuid4())
+            self.session = {"last_access": time.time()}
+            self.sessions[session_id] = self.session
+            request_handler.send_header(
+                "Set-Cookie", f"session_id={session_id}; Path=/; HttpOnly; SameSite=Strict;"
+            )
+
+        self.request = method
+        self.body_params = {}
+        self.files = {}
+
+        if method == "POST":
+            try:
+                content_type = environ.get("CONTENT_TYPE", "")
+                content_length = int(environ.get("CONTENT_LENGTH", 0) or 0)
+                body = environ["wsgi.input"].read(content_length)
+
+                if "multipart/form-data" in content_type:
+                    self.parse_multipart(body, content_type)
+                else:
+                    body_str = body.decode("utf-8", "ignore")
+                    self.body_params = parse_qs(body_str)
+            except Exception as e:
+                start_response("400 Bad Request", [("Content-Type", "text/html")])
+                return [f"400 Bad Request: {str(e)}".encode("utf-8")]
+
+        sig = inspect.signature(handler_function)
+        func_args = []
+
+        for param in sig.parameters.values():
+            if self.path_params:
+                func_args.append(self.path_params.pop(0))
+            elif param.name in self.query_params:
+                func_args.append(self.query_params[param.name][0])
+            elif param.name in self.body_params:
+                func_args.append(self.body_params[param.name][0])
+            elif param.name in self.files:
+                func_args.append(self.files[param.name])
+            elif param.name in self.session:
+                func_args.append(self.session[param.name])
+            elif param.default is not param.empty:
+                func_args.append(param.default)
+            else:
+                msg = f"400 Bad Request: Missing required parameter '{param.name}'"
+                start_response("400 Bad Request", [("Content-Type", "text/html")])
+                return [msg.encode("utf-8")]
+
+        if handler_function == getattr(self, "index", None) and not func_args and path:
+            start_response("404 Not Found", [("Content-Type", "text/html")])
+            return [b"404 Not Found"]
+
+        try:
+            response = handler_function(*func_args)
+            status_code = 200
+            response_body = response
+            extra_headers = []
+
+            if isinstance(response, tuple):
+                if len(response) == 2:
+                    status_code, response_body = response
+                elif len(response) == 3:
+                    status_code, response_body, extra_headers = response
+                else:
+                    start_response("500 Internal Server Error", [("Content-Type", "text/html")])
+                    return [b"500 Internal Server Error: Invalid response tuple"]
+
+            status_map = {
+                206: "206 Partial Content",
+                302: "302 Found",
+                404: "404 Not Found",
+                500: "500 Internal Server Error",
+            }
+            status_str = status_map.get(status_code, f"{status_code} OK")
+            headers = request_handler._headers_to_send
+            headers.extend(extra_headers)
+            if not any(h[0].lower() == "content-type" for h in headers):
+                headers.append(("Content-Type", "text/html; charset=utf-8"))
+
+            start_response(status_str, headers)
+
+            if hasattr(response_body, "__iter__") and not isinstance(response_body, (bytes, str)):
+                def byte_stream(gen: Any) -> Any:
+                    for chunk in gen:
+                        if isinstance(chunk, str):
+                            yield chunk.encode("utf-8")
+                        else:
+                            yield chunk
+                return byte_stream(response_body)
+
+            if isinstance(response_body, str):
+                response_body = response_body.encode("utf-8")
+
+            return [response_body]
+
+        except Exception as e:
+            print(f"Error processing request: {e}")
+            try:
+                start_response("500 Internal Server Error", [("Content-Type", "text/html")])
+            except:
+                pass
+            return [b"500 Internal Server Error"]
+
+    def parse_multipart(self, body: bytes, content_type: str) -> None:
+        boundary = None
+        parts = content_type.split(";")
+        for part in parts:
+            part = part.strip()
+            if part.startswith("boundary="):
+                boundary = part.split("=", 1)[1]
+                break
+
+        if not boundary:
+            raise ValueError("Boundary not found in Content-Type header.")
+
+        boundary_bytes = boundary.encode("utf-8")
+        delimiter = b'--' + boundary_bytes
+        end_delimiter = b'--' + boundary_bytes + b'--'
+
+        sections = body.split(delimiter)
+        for section in sections:
+            if not section or section == b'--' or section == b'--\r\n':
+                continue
+            if section.startswith(b'\r\n'):
+                section = section[2:]
+            if section.endswith(b'\r\n'):
+                section = section[:-2]
+            if section == b'--':
+                continue
+
+            try:
+                headers, content = section.split(b'\r\n\r\n', 1)
+            except ValueError:
+                continue
+
+            headers = headers.decode("utf-8", "ignore").split("\r\n")
+            header_dict = {}
+            for header in headers:
+                if ':' in header:
+                    key, value = header.split(':', 1)
+                    header_dict[key.strip().lower()] = value.strip()
+
+            disposition = header_dict.get("content-disposition", "")
+            disposition_parts = disposition.split(";")
+            disposition_dict = {}
+            for disp_part in disposition_parts:
+                if "=" in disp_part:
+                    key, value = disp_part.strip().split("=", 1)
+                    disposition_dict[key] = value.strip('"')
+
+            name = disposition_dict.get("name")
+            filename = disposition_dict.get("filename")
+
+            if filename:
+                file_content_type = header_dict.get("content-type", "application/octet-stream")
+                file_data = content
+                self.files[name] = {
+                    'filename': filename,
+                    'content_type': file_content_type,
+                    'data': file_data
+                }
+            elif name:
+                value = content.decode("utf-8", "ignore")
+                if name in self.body_params:
+                    self.body_params[name].append(value)
+                else:
+                    self.body_params[name] = [value]
diff --git a/examples/requests/app.py b/examples/requests/app.py
index e996a5a..5ca8baf 100644
--- a/examples/requests/app.py
+++ b/examples/requests/app.py
@@ -3,19 +3,19 @@ from MicroPie import Server
 # Request handlers defined outside the class
 
 def get_handler():
-    return b"Hello, GET request received!"  # Convert to bytes
+    return b"Hello, GET request received!"
 
 def post_handler():
-    return b"Hello, POST request received!"  # Convert to bytes
+    return b"Hello, POST request received!"
 
 def put_handler():
-    return b"Hello, PUT request received!"  # Convert to bytes
+    return b"Hello, PUT request received!"
 
 def patch_handler():
-    return b"Hello, PATCH request received!"  # Convert to bytes
+    return b"Hello, PATCH request received!"
 
 def delete_handler():
-    return b"Hello, DELETE request received!"  # Convert to bytes
+    return b"Hello, DELETE request received!"
 
 def head_handler():
     # Return status 200 with empty body and proper headers
diff --git a/examples/streaming/livestream.py b/examples/streaming/livestream.py
new file mode 100644
index 0000000..f4a4752
--- /dev/null
+++ b/examples/streaming/livestream.py
@@ -0,0 +1,51 @@
+import socketio
+import eventlet
+from MicroPie import Server
+
+sio = socketio.Server(cors_allowed_origins="*")
+app = Server()
+active_users = set()
+
+class MyApp(Server):
+    def index(self):
+        return self.render_template('index.html')
+
+    def submit(self, username, action):
+        if username:
+            active_users.add(username)
+            if action == 'Start Streaming':
+                return self.redirect(f'/stream/{username}')
+            elif action == 'Watch Stream':
+                return self.redirect(f'/watch/{username}')
+        return self.redirect('/')
+
+    def stream(self, username):
+        if username not in active_users:
+            return self.redirect('/')
+        return self.render_template('stream.html', username=username)
+
+    def watch(self, username):
+        if username not in active_users:
+            return self.redirect('/')
+        return self.render_template('watch.html', username=username)
+
[email protected]
+def connect(sid, environ):
+    print(f"Client {sid} connected")
+
[email protected]
+def disconnect(sid):
+    print(f"Client {sid} disconnected")
+
[email protected]
+def stream(sid, data):
+    username = data.get('username')
+    frame = data.get('frame')
+    sio.emit('broadcast', {'username': username, 'frame': frame}, skip_sid=sid)
+
+def run_server():
+    eventlet.wsgi.server(eventlet.listen(('0.0.0.0', 5000)), socketio.WSGIApp(sio, MyApp().wsgi_app))
+
+if __name__ == '__main__':
+    run_server()
+
diff --git a/examples/streaming/templates/index.html b/examples/streaming/templates/index.html
new file mode 100644
index 0000000..b710858
--- /dev/null
+++ b/examples/streaming/templates/index.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Webcam Streaming</title>
+</head>
+<body>
+    <h1>Webcam Streaming App</h1>
+    <form method="post" action="/submit">
+        <input type="text" name="username" placeholder="Enter username" required>
+        <button type="submit" name="action" value="Start Streaming">Start Streaming</button>
+        <button type="submit" name="action" value="Watch Stream">Watch Stream</button>
+    </form>
+</body>
+</html>
+
diff --git a/examples/streaming/templates/stream.html b/examples/streaming/templates/stream.html
new file mode 100644
index 0000000..a8a5e0c
--- /dev/null
+++ b/examples/streaming/templates/stream.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Streaming: {{ username }}</title>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
+</head>
+<body>
+    <h1>Streaming as {{ username }}</h1>
+    <video id="webcam" autoplay playsinline></video>
+
+    <script>
+        const socket = io();
+        const username = "{{ username }}";
+        socket.emit('join', { username: username });
+
+        async function startWebcam() {
+            const stream = await navigator.mediaDevices.getUserMedia({ video: true });
+            document.getElementById('webcam').srcObject = stream;
+
+            const canvas = document.createElement('canvas');
+            const context = canvas.getContext('2d');
+
+            setInterval(() => {
+                context.drawImage(document.getElementById('webcam'), 0, 0, canvas.width, canvas.height);
+                let frame = canvas.toDataURL('image/webp');
+                socket.emit('stream', { username: username, frame: frame });
+            }, 100);
+        }
+
+        startWebcam();
+    </script>
+</body>
+</html>
+
diff --git a/examples/streaming/templates/watch.html b/examples/streaming/templates/watch.html
new file mode 100644
index 0000000..3cf8f2a
--- /dev/null
+++ b/examples/streaming/templates/watch.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1.0">
+    <title>Watching: {{ username }}</title>
+    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/4.0.1/socket.io.js"></script>
+</head>
+<body>
+    <h1>Watching Stream of {{ username }}</h1>
+    <img id="videoFeed" style="width: 100%; max-width: 800px;">
+
+    <script>
+        const socket = io();
+        const username = "{{ username }}";
+        socket.emit('join', { username: username });
+
+        socket.on('broadcast', (data) => {
+            document.getElementById('videoFeed').src = data.frame;
+        });
+    </script>
+</body>
+</html>
+
diff --git a/examples/todolist/app.py b/examples/todolist/app.py
index a984f0a..fbd8ccc 100644
--- a/examples/todolist/app.py
+++ b/examples/todolist/app.py
@@ -67,7 +67,8 @@ class ToDoApp(Server):
             if self.users.get(username) == password:
                 self.session.update({"logged_in": True, "username": username})
                 return self.redirect("/")
-            return self.render_template("login.html", error="Invalid credentials")
+            return self.render_template("login.html",
+                                        error="Invalid credentials")
 
     def logout(self):
         """GET /logout -> Clear session and redirect to login."""
diff --git a/setup.py b/setup.py
index 23cabab..c861616 100644
--- a/setup.py
+++ b/setup.py
@@ -33,9 +33,18 @@ setup(name="MicroPie",
     license="three-clause BSD",
     url="http://github.com/patx/micropie",
     classifiers = [
-        "Programming Language :: Python",
-        "License :: OSI Approved :: BSD License",
-        "Intended Audience :: Developers"],
+    "Programming Language :: Python :: 3",
+    "Environment :: Web Environment",
+    "Intended Audience :: Developers",
+    "License :: OSI Approved :: BSD License",
+    "Operating System :: OS Independent",
+    "Programming Language :: Python",
+    "Topic :: Internet :: WWW/HTTP :: Dynamic Content",
+    "Topic :: Internet :: WWW/HTTP :: WSGI",
+    "Topic :: Internet :: WWW/HTTP :: HTTP Servers",
+    "Topic :: Internet :: WWW/HTTP :: WSGI :: Application",
+    "Topic :: Software Development :: Libraries :: Application Frameworks",
+    "Typing :: Typed"],
     py_modules=['MicroPie'],
     install_requires=['jinja2'],
 )