Skip to content

Instantly share code, notes, and snippets.

@twisteroidambassador
twisteroidambassador / ip_network_subnets.py
Created November 2, 2021 15:59
Why is picking a random IP subnet in Python so awkward?
import ipaddress
import typing
import abc
IPNetworkType = typing.TypeVar('IPNetworkType', ipaddress.IPv4Network, ipaddress.IPv6Network)
class IPSubnetSequence(typing.Sequence[IPNetworkType]):
"""An indexable sequence of subnets of a given network.
@twisteroidambassador
twisteroidambassador / distinguish_cancellation.py
Created February 20, 2019 03:15
Determine whether task was cancelled from inside or outside
import asyncio
class CancelledFromOutside(asyncio.CancelledError):
pass
class CancelledFromInside(asyncio.CancelledError):
pass
@twisteroidambassador
twisteroidambassador / gnlpy_tcpmetrics.py
Created June 24, 2018 14:02
Getting tcp_metrics with gnlpy
"""Accessing TCP metrics in Linux using Generic Netlink via gnlpy."""
import array
import ipaddress
import struct
from pprint import pprint
from typing import Union
import gnlpy.netlink as netlink
@twisteroidambassador
twisteroidambassador / barrier.py
Created April 11, 2017 10:00
An (partial) asyncio version of threads.Barrier. There's only wait(), no reset() or abort() or concept of being broken, and like all other asyncio->threading analogues, no timeouts.
import asyncio
class Barrier():
"""asyncio version of threading.Barrier (partial).
Implements wait() only, without support for timeouts."""
def __init__(self, parties, loop=None):
if loop is None:
self._loop = asyncio.get_event_loop()