Skip to content

Instantly share code, notes, and snippets.

View kgriffs's full-sized avatar

Kurt Griffiths kgriffs

View GitHub Profile
@kgriffs
kgriffs / rdpy_demo.py
Last active June 13, 2023 09:05
RDP Automation Hack
import time
from rdpy.protocol.rdp import rdp
from twisted.internet import reactor
class MyRDPFactory(rdp.ClientFactory):
def clientConnectionLost(self, connector, reason):
reactor.stop()
@kgriffs
kgriffs / __main__.py
Created September 14, 2016 15:52 — forked from jmvrbanac/__main__.py
Example Custom Gunicorn Application
"""
Sample Application
Usage:
sample [options]
Options:
-h --help Show this screen.
"""
import aumbry
@kgriffs
kgriffs / falcon-asgi-ws-interface.py
Last active April 30, 2023 11:20
Falcon ASGI+WebSocket Interface Proposal (By Example)
# See also:
#
# * https://asgi.readthedocs.io/en/latest/specs/www.html#websocket
# * https://developer.mozilla.org/en-US/docs/Web/API/Websockets_API
#
import falcon.asgi
import falcon.media
@kgriffs
kgriffs / disable_echo.py
Created June 7, 2013 00:52
Disable echo in Python
def enable_echo(enable):
fd = sys.stdin.fileno()
new = termios.tcgetattr(fd)
if enable:
new[3] |= termios.ECHO
else:
new[3] &= ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, new)
@kgriffs
kgriffs / results.py
Created May 21, 2021 23:05
Benchmarking Python JSON libs: std vs. orjson vs. simdjson - simple encode/decode tests
# ----------------------------------------------------------------------------
import json
import simdjson
import libpy_simdjson
decoder_std = json.JSONDecoder()
encoder_std = json.JSONEncoder(ensure_ascii=False)
p = simdjson.Parser()
p2 = libpy_simdjson.Parser()
@kgriffs
kgriffs / async-does-not-mix.py
Last active November 18, 2022 21:22
Example demonstrating how a synchronous control flow never yields to tasks on the event loop, starving them.
import asyncio
import httpx
import time
async def do_work_async():
http = httpx.AsyncClient()
while True:
resp = await http.get('https://httpbin.org/status/202')
@kgriffs
kgriffs / aiobotocore-config-example.py
Last active November 7, 2022 16:39
aiobotocore config example (vs. boto3) for setting connect_timeout and usign the standard retries mode
import asyncio
import aiobotocore.session
import aiobotocore.config
async def example():
# Equivalent to boto3.client('s3', config=botocore.client.Config(**kwargs))
# See also for available options:
# * https://botocore.amazonaws.com/v1/documentation/api/latest/reference/config.html
@kgriffs
kgriffs / example_asyncio_signal_shutdown.py
Last active November 2, 2022 01:52
Example showing how to trap a signal with a Python asyncio coroutine function and exit cleanly without logging an error.
import asyncio
import functools
import signal
import sys
import time
async def worker(shutdown_event):
try:
while True:
@kgriffs
kgriffs / example.sh
Last active August 24, 2022 03:18
Start redis server without disk persistence; memory only for testing (avoids having to clean up a dump.rdb file)
redis-server --save "" --appendonly no
@kgriffs
kgriffs / listening.sh
Created July 29, 2022 22:01
macOS port listening bash
# https://stackoverflow.com/a/30029855/21784
listening() {
if [ $# -eq 0 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P
elif [ $# -eq 1 ]; then
sudo lsof -iTCP -sTCP:LISTEN -n -P | grep -i --color $1
else
echo "Usage: listening [pattern]"
fi
}