Skip to content

Instantly share code, notes, and snippets.

View vytas7's full-sized avatar

Vytautas Liuolia vytas7

View GitHub Profile
@vytas7
vytas7 / threaded_server.py
Last active August 10, 2024 23:18
Testing Falcon with free-threaded CPython 3.13
import http
import logging
# import random
import socketserver
import threading
# import time
import wsgiref.simple_server
import falcon
@vytas7
vytas7 / aioboto3_upload.py
Created July 26, 2022 08:12
Upload files from a multipart form directly to S3 (Falcon ASGI)
import logging
import aioboto3
import falcon.asgi
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO
)
@vytas7
vytas7 / async_deps.py
Last active May 29, 2022 13:29
Falcon: inject params from async context managers
import logging
import aiohttp
import falcon
import falcon.asgi
# NOTE(vytas): Useful since ASGI otherwise has nothing like wsgierrors.
logging.basicConfig(
format='%(asctime)s [%(levelname)s] %(message)s', level=logging.INFO)
@vytas7
vytas7 / texthandler.py
Last active March 12, 2022 18:40
text/plain media handler for Falcon
import cgi
import functools
import falcon
class TextHandler(falcon.media.BaseHandler):
DEFAULT_CHARSET = 'utf-8'
@classmethod
@vytas7
vytas7 / hybridapp.py
Last active February 8, 2022 15:46
Hybrid ASGI/WSGI app in the same process
#!/usr/bin/env python
import asyncio
import logging
import threading
import uuid
import falcon
import falcon.asgi
import gunicorn.app.base
import uvicorn
@vytas7
vytas7 / proxy.py
Created August 17, 2021 16:32
Proxying a website with Falcon ASGI + httpx
import falcon.asgi
import httpx
class Proxy(object):
UPSTREAM = 'https://falconframework.org'
def __init__(self):
self._client = httpx.AsyncClient()
@vytas7
vytas7 / instrumentation.py
Created April 20, 2021 06:48
Discussed on falconry/user: how to implement a basic instrumentation plugin using middleware
import random
import time
import uuid
import falcon
class RequestMonitor:
def __init__(self, req, resp):
@vytas7
vytas7 / error_handling.py
Last active March 22, 2024 19:39
Falcon error handling examples
import falcon
import falcon.routing
class FloatConverter(falcon.routing.BaseConverter):
def convert(self, value):
try:
return float(value)
except ValueError:
return None
@vytas7
vytas7 / background_queue_falcon.py
Created March 21, 2021 20:18 — forked from kgriffs/background_queue_falcon.py
Pattern for saving / doing background work from a Falcon App using a queue from the standard Python Library
# Credit: @vytas7 (Vytautas Liuolia)
import queue
import signal
import threading
import time
import uuid
import falcon
@vytas7
vytas7 / test.py
Last active March 22, 2021 07:45
Testing a simple Falcon+MongoDB document API
# # docker run --rm -it -p 27017:27017 mongo
# $ gunicorn --workers 8 --worker-class=meinheld.gmeinheld.MeinheldWorker test:app
import itertools
import random
import falcon
import pymongo