Skip to content

Latest commit

 

History

History
86 lines (56 loc) · 1.56 KB

README.md

File metadata and controls

86 lines (56 loc) · 1.56 KB

Cosmo

Cosmo is an asynchronous python webserver that utilizes raw sockets to send and receive data, without using ASGI/WSGI.

Benchmarking

Connections Handled per Second

Examples

Simple Server

from cosmo import App, Request, Response

app = App("0.0.0.0", 8080)

@app.route("/", "text/html")
async def index(request: Request):
    return Response(f"<h1>{request.address}</h1>")

app.serve()

Using Custom Headers

from cosmo import App, Request, Response

app = App("0.0.0.0", 8080)

@app.route("/", "text/html")
async def index(request: Request):
    headers = {"x-custom-header": "custom"}
    content = "<h1>Custom Headers: </h1>\n<ul>"
    for header in headers.keys():
        content += f"<li>{header}: {headers[header]}</li>\n"
    content += "</ul>"
    return Response(content, headers)

app.serve()

Returning an HTML Page

from cosmo import App, html_page, Request, Response

app = App("0.0.0.0", 8080)

@app.route("/", "text/html")
async def index(request: Request):
    return Response(html_page("path/to/page.html"))

app.serve()

Using Routes from a different file

In router.py:

from cosmo import Request, Response, Router

router = Router()

@router.route("/")
async def index():
    return Response("<h1>Hi</h1>")

In app.py:

from cosmo import App, Request, Response

from router import router

app = App("0.0.0.0", 8080)

app.import_router(router)

app.serve()

Docs

Coming soon...