Skip to content

Instantly share code, notes, and snippets.

View monperrus's full-sized avatar

Martin Monperrus monperrus

View GitHub Profile
@monperrus
monperrus / example-flask.py
Created March 16, 2025 10:30
a simple python application which expose an example service hello
from flask import Flask, request, jsonify
from datetime import datetime
app = Flask(__name__)
@app.route('/')
def root():
return jsonify({
'message': 'Welcome to the Hello Service',
'endpoints': {
@monperrus
monperrus / example-ncurse.py
Created March 16, 2025 10:30
a python ncurse app with two column
import curses
import time
def main(stdscr):
# Turn off cursor blinking
curses.curs_set(0)
# Color pair definitions
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_BLACK, curses.COLOR_WHITE)
@monperrus
monperrus / http-server.py
Created March 16, 2025 10:30
in python 3, start an http server, when a request is received, save the GET query parameter in file code.txt and shutdown the server
from http.server import BaseHTTPRequestHandler, HTTPServer
import urllib.parse
class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Parse the query parameters from the URL
parsed_path = urllib.parse.urlparse(self.path)
query_params = urllib.parse.parse_qs(parsed_path.query)
# Extract the 'code' parameter if it exists
@monperrus
monperrus / example-wsgi-input.py
Created March 16, 2025 10:30
pipe wsgi.input into subprocess.Popen
import subprocess
def application(environ, start_response):
# Get the wsgi.input stream
input_stream = environ['wsgi.input']
# Create a subprocess
process = subprocess.Popen(
['some_command'], # Replace with your command
stdin=subprocess.PIPE,
@monperrus
monperrus / chinese.py
Created March 16, 2025 10:30
implement the chinese remainder theorem in python
def extended_euclidean(a, b):
if a == 0:
return b, 0, 1
else:
g, x, y = extended_euclidean(b % a, a)
return g, y - (b // a) * x, x
def chinese_remainder(n, a):
sum = 0
prod = 1
@monperrus
monperrus / osmtosvg.py
Created March 16, 2025 10:30
write a transformer xml in osm format (openstreetmap) to svg which only keeps map lines
import xml.etree.ElementTree as ET
from typing import List, Dict, Tuple
import sys
class OSMtoSVGConverter:
def __init__(self):
self.nodes: Dict[str, Tuple] = {}
self.ways: List[List[Tuple]] = []
self.min_lat = float('inf')
self.max_lat = float('-inf')
@monperrus
monperrus / clean_bash_history.py
Created March 16, 2025 10:07
a python script to remove secrets and keys in bash history file
#!/usr/bin/env python3
# cleans bash history
# generated with Claude on March 2025
# does not work out of the box
import os
import re
import shutil
import argparse
from datetime import datetime
@monperrus
monperrus / dns-over-https.py
Created March 16, 2025 10:07
a python script that performs dns resolution on 8.8.8.8 doh
#!/usr/bin/python3
# resolve names over DNS-over-HTTPS port 443 (google server by default)
import requests
import json
import sys
def resolve_dns_over_https(domain, dns_type="A"):
"""
Perform DNS resolution using Google's DNS-over-HTTPS service
@monperrus
monperrus / create-pdf-photos.py
Created March 16, 2025 10:07
small python script to concatenate pictures as jpg files into a PDF of the same size
#!/usr/bin/python3
# small python script to concatenate pictures as jpg files into a PDF of the same size
# Nov 2024, chatgpt
# pip3 install reportlab
import os
from PIL import Image
from reportlab.pdfgen import canvas
import sys
@monperrus
monperrus / systray-notes.py
Created March 16, 2025 10:07
a python systray application that adds a list of items in system tray icon
#!/usr/bin/python
# a systray app for my notes (tomboy + joplin)
import pystray
from PIL import Image
import tkinter as tk
from tkinter import simpledialog
import os
import joplin_notes
import tomboy