Skip to content

Instantly share code, notes, and snippets.

View kissgyorgy's full-sized avatar

György Kiss kissgyorgy

View GitHub Profile
@kissgyorgy
kissgyorgy / asgi.py
Last active April 2, 2024 16:28
Django: Run ASGI application with Werkzeug's DebuggedApplication (traceback on Exception)
import os
from a2wsgi import WSGIMiddleware
from django.core.wsgi import get_wsgi_application
from django.views import debug as django_views_debug
from django_extensions.management.technical_response import (
null_technical_500_response,
)
from werkzeug.debug import DebuggedApplication
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "core.settings")
@kissgyorgy
kissgyorgy / http_over_ssh_proxy.py
Last active December 24, 2023 16:35
Python: HTTP over SSH
import asyncio
import sys
from typing import Optional
import asyncssh
import httpx
class MySSHTCPSession(asyncssh.SSHTCPSession):
def __init__(self):
@kissgyorgy
kissgyorgy / mount-disk.service
Created July 24, 2023 13:21
WSL: Automatically mount disk on WSL startup
[Unit]
Description = Mount disk
Before = local-fs-pre.target
[Service]
User = root
Type = oneshot
RemainAfterExit = yes
ExecStart = /usr/local/sbin/mount-disk.sh
@kissgyorgy
kissgyorgy / bitarray.js
Created January 10, 2023 00:20
JavaScript: BitArray
import { inflateRaw, deflateRaw } from "pako";
// https://en.wikipedia.org/wiki/Run-length_encoding
const Z_RLE = 3;
class BitArray {
constructor(bitSize) {
const remainder = Math.min(1, bitSize % 8);
const byteSize = Math.floor(bitSize / 8) + remainder;
const buffer = new ArrayBuffer(byteSize);
@kissgyorgy
kissgyorgy / 55-bytes-of-css.md
Created October 1, 2022 22:31 — forked from JoeyBurzynski/55-bytes-of-css.md
58 bytes of css to look great nearly everywhere

58 bytes of CSS to look great nearly everywhere

When making this website, i wanted a simple, reasonable way to make it look good on most displays. Not counting any minimization techniques, the following 58 bytes worked well for me:

main {
  max-width: 38rem;
  padding: 2rem;
  margin: auto;
}
#!/usr/bin/python3
import sys
import asyncio
import greenlet
class AsyncIoGreenlet(greenlet.greenlet):
def __init__(self, driver, fn):
greenlet.greenlet.__init__(self, fn, driver)
self.driver = driver
@kissgyorgy
kissgyorgy / prefixed_string_template.py
Created July 20, 2022 00:32
Python: use prefix in a string.Template
from string import Template
class EnvTemplate(Template):
"""Substitute variables regularly, but environment variables need "env." prefix,
and only with the braced syntax.
For example: ${env.HOME} works, but $env.HOME does nothing.
"""
braceidpattern = rf"(?:env\.)?{Template.idpattern}"
@kissgyorgy
kissgyorgy / which-country.py
Created July 10, 2022 12:40
Python: Which country an IP address resides in
from pathlib import Path
from typing import Optional, Union, NewType, cast, Tuple, Iterator
from ipaddress import (
IPv6Address,
IPv6Network,
ip_address,
ip_network,
IPv4Address,
IPv4Network,
)
@kissgyorgy
kissgyorgy / shield_and_block.py
Created July 6, 2022 12:16
Python: shield a coroutine from cancellation and wait until completely finished
import asyncio
from typing import Coroutine
async def force_complete(coro: Coroutine):
"""Shield the Coroutine from cancellation and block the calling coroutine until it's fully completed."""
task = asyncio.create_task(coro)
try:
await asyncio.shield(task)
except asyncio.CancelledError:
@kissgyorgy
kissgyorgy / parse_der_cert.py
Created July 2, 2022 17:20
Python: Parse X.509 DER certificate with extra data at the end
from pathlib import Path
from cryptography import x509
ASN1_SEQUENCE_TAG = 0x30
def get_der_cert_length(cert_bytes: bytes) -> int:
if cert_bytes[0] != ASN1_SEQUENCE_TAG:
raise ValueError("Not a DER certificate")