PyProject 1

Sample PyProject from: PyProject poetry init poetry add motor pydantic poetry add mkdocs-material mkdocstrings[python] mypy ruff poethepoet pytest pytest-asyncio pytest-random-order --group dev [project] name = "motorhead" description = "Async MongoDB with vanilla Pydantic v2+ - made easy." readme = "" license = { text = "MIT" } authors = [ { name = "Peter Volf", email = "" }, ] requires-python = ">=3.10" dependencies = ["pydantic", "motor"] classifiers = [ "Intended Audience :: Information Technology", "Operating System :: OS Independent", "Programming Language :: Python :: 3", "Development Status :: 4 - Beta", "Topic :: Internet", "Topic :: Software Development :: Libraries", "Topic :: Software Development", "Typing :: Typed", "Environment :: Web Environment", "Framework :: FastAPI", "Intended Audience :: Developers", "License :: OSI Approved :: MIT License", "Topic :: Internet :: WWW/HTTP", ] [project.

Python Twisted

WebServer with Python Twisted cat << 'EOF' > from twisted.web import server, resource from twisted.internet import reactor, endpoints class Counter(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): client_ip = request.getClientAddress().host r=request.uri.decode('utf-8') if not r =="/favicon.ico": self.numberRequests += 1 request.setHeader(b"content-type", b"text/plain") content = u"I am request #{} from {}\n".format(self.numberRequests, client_ip) return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) EOF Run poetry init poetry add twisted poetry run python Browse Open your Browser: http://ip-of-your-host:8080

Fastapi Simple Security

How to Protect your App with Simple Security Let’s build a small API Endpoint with FastAPI and protect it with SimpleSecurity. API key based security package for FastAPI, focused on simplicity of use: Full functionality out of the box, no configuration required API key security with local sqlite backend, working with both header and query parameters Default 15 days deprecation for generated API keys Key creation, revocation, renewing, and usage logs handled through administrator endpoints No dependencies, only requiring FastAPI and the python standard library Build new App and show the Directory Structure

Python Ping3

Need a Litte Ping Function ? Test cat <<'EOF'> import argparse from ping3 import ping, verbose_ping def do_ping(host: str, timeout: int = 3, size: int = 1500, output: str = "json"): # output: json|txt # '21.54 ms' if size > 1500: size = 1500 result = ( str( round( ping(dest_addr=host, timeout=timeout, size=size, unit="ms"), 2, ) ) + " ms" ) if output.lower() == "json": return {"host": host, "timeout": timeout, "size": size, "result": result} if output.

Python Logger

a custom logger for Python let’s tune the default logger a bit so he write nice and colored messages. Screenshot a little config File … cat <<'EOF'> LOGGER_MAX_FILE_LENGTH = 10 EOF src/ the logger code in the ‘src’ Folder mkdir src cat <<'EOF'> src/ import logging import datetime import sys from config import * if isinstance(LOGGER_MAX_FILE_LENGTH, int): LOGGER_MAX_FILE_LENGTH = str(LOGGER_MAX_FILE_LENGTH) def get_now() -> str: # # choose your format # current_time = datetime.

Flask JWT - Sample

Flask & JWT getting your hands dirty with Flask and JWT Source with some modifications by myself … Environment Test under macOS & OpenBSD, Poetry installed and working Script build virtual env export app="app100" export FLASK_APP="${app}/app" poetry new ${app} cd ${app} set python 3.10 poetry env use $(which python3.10) gsed -i "s/python = \"^3.*$/python = \"^3.10\"/" pyproject.toml poetry lock add packages wget -4 -O requirements.txt echo "marshmallow-sqlalchemy" >> requirements.