Skip to content

Instantly share code, notes, and snippets.

View fphammerle's full-sized avatar

Fabian Peter Hammerle fphammerle

View GitHub Profile
@fphammerle
fphammerle / python_http_client_connection_unix_socket.py
Created December 30, 2021 11:28
Send HTTP request to unix socket with python's built-in http.client.HttpConnection class
# tested on cpython v3.9.5
import http.client
import json
import pathlib
import socket
class _HttpConnectionUnixSocket(http.client.HTTPConnection):
def __init__(self, path: pathlib.Path) -> None:
@fphammerle
fphammerle / debian-debootstrap-grub-uefi.sh
Created August 27, 2021 12:21 — forked from superboum/LICENCE.txt
Install Debian with Debootstrap + Grub EFI
#!/bin/bash
set -e # Exit on error
DEVICE=$1
[ -z "${DEVICE}" ] && echo "Usage $0 /dev/sdX" && exit 1
udevadm info -n ${DEVICE} -q property
echo "Selected device is ${DEVICE}"
read -p "[Press enter to continue or CTRL+C to stop]"
# https://forum.pine64.org/showthread.php?tid=1297&pid=15958#pid15958
$ fdtget --type x /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/eth@01c30000 reg
0 1c30000 0 10000 0 1c00000 0 30
# https://forum.pine64.org/showthread.php?tid=1297&pid=16138#pid16138
$ fdtget /boot/pine64/sun50i-a64-pine64-plus.dtb /aliases spi0
/soc@01c00000/spi@01c68000
$ fdtget /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/spi@01c68000 status
disabled
$ sudo fdtput --verbose --type s /boot/pine64/sun50i-a64-pine64-plus.dtb /soc@01c00000/spi@01c68000 status okay
#!/usr/bin/env python3
import os
import select
DEFAULT_BUFFER_SIZE_BYTES = 8196
DEFAULT_READ_TIMEOUT_SECONDS = 1.0
def rselect(fd, timeout_seconds=None):
#!/usr/bin/env python3
"""
> sstream = io.StringIO()
> subprocess.run(['hostname'], stdout=sstream, check=True)
io.UnsupportedOperation: fileno
"""
import os
import select
@fphammerle
fphammerle / libpam-authenticate.c
Last active May 20, 2018 11:32
Authenticate user via libpam.
#include <assert.h>
#include <security/pam_appl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // getpass
// apt install libpam0g-dev
// compile with flag -lpam
// gcc -lgcrypt ...
#include <assert.h>
#include <gcrypt.h>
#include <stdio.h>
#include <stdlib.h>
const size_t KEYGRIP_LENGTH = 20;
unsigned char* read_binary(const char* path, size_t* bytes_read) {
@fphammerle
fphammerle / xlib-xrandr-ctypes-print-output-names.py
Created January 21, 2018 15:33
Display names of outputs available to the X server by accessing the xrandr extension via python's ctypes
#!/usr/bin/env python3
"""
Display names of outputs available to the X server similiar to 'xrandr' command.
Access xlib and xrandr extension via python's ctypes library.
tags: x11, xorg, x.org, xwindow-system, xlib, libx, xrandr, outputs, monitor, display
"""
import ctypes
@fphammerle
fphammerle / xlib-non-blocking-event-loop.py
Last active January 14, 2018 13:13
Wrap python-xlib's Xlib.display.next_event() so it does not block the main loop
"""
python-xlib's Xlib.display.next_event() does not have a timeout when waiting for an event to be queued.
It may block the script's execution forever.
Workaround:
Call display.next_event() only if an event is available in the queue.
Use select.select() on the xserver's socket to wait for an event and specify a timeout.
based on: https://stackoverflow.com/a/8592969/5894777
@fphammerle
fphammerle / sabre-dav-yii2-auth-backend.php
Last active July 21, 2017 09:31
use yii2's authentication capabilities to control access to SabreDAV's webdav server
<?php
use \Sabre\HTTP\RequestInterface;
use \Sabre\HTTP\ResponseInterface;
class SabreDavYii2AuthBackend implements \Sabre\DAV\Auth\Backend\BackendInterface
{
function check(RequestInterface $request, ResponseInterface $response)
{
if(\Yii::$app->user->isGuest) {