Skip to content

Instantly share code, notes, and snippets.

View sourceperl's full-sized avatar

l.lefebvre sourceperl

  • Hauts-de-France
View GitHub Profile
@sourceperl
sourceperl / serial_test.py
Created April 30, 2025 16:07
Test of the RevPi Connect 4 RS485 interface with python serial.
#!/usr/bin/env python3
from serial import Serial
from serial.rs485 import RS485Settings
# Configure the serial port with RS485 settings (with RTS level set for tx and rx)
s = Serial(port='/dev/ttyRS485', baudrate=9600, bytesize=8, parity='N', stopbits=1)
s.rs485_mode = RS485Settings(rts_level_for_tx=False, rts_level_for_rx=True)
try:
@sourceperl
sourceperl / nginx_log_parser.py
Created March 24, 2025 10:41
An example of a Python Nginx access log parser with gzip support (logrotate compatible).
#!/usr/bin/env python3
""""
Decode nginx access logs (plain text or gzipped).
To analyze all available logs:
$ this_script /var/log/nginx/access.log*
"""
@sourceperl
sourceperl / main.py
Last active January 8, 2025 18:25
micropython aioble basic peripheral (service environmental_sensing with temperature characteristic)
import uasyncio as aio
import aioble
from bluetooth import UUID
import random
import struct
# org.bluetooth.service.environmental_sensing
_ENV_SENSE_UUID = UUID(0x181A)
# org.bluetooth.characteristic.temperature
@sourceperl
sourceperl / multi_uid.py
Created November 5, 2024 17:07
A basic example for serving multiple unit IDs on the same server
import time
from collections import defaultdict
from typing import Dict
from pyModbusTCP.server import DataBank, ModbusServer
class MyDataBank(DataBank):
class Data:
def __init__(self) -> None:
@sourceperl
sourceperl / multithreaded_ping.py
Created April 12, 2024 18:51
Basic multithreaded ping with python (on Linux systems)
from dataclasses import dataclass
from datetime import datetime
from queue import Empty, Queue
import re
import subprocess
from threading import Thread
import time
@dataclass
@sourceperl
sourceperl / sendmail.py
Created May 12, 2023 16:36
Send an mail to smtp.free.fr server
#!/usr/bin/env python3
""" Send mail with SMTP protocol. """
import logging
import re
import smtplib
from typing import List, Union
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@sourceperl
sourceperl / main.py
Last active December 23, 2022 16:18
A micropython example of HTTP (json) data retrieval with handling of wifi connection errors on the Pico W board.
import uasyncio as aio
from machine import Pin
import network
import rp2
import errno
import urequests
import gc
from private_data import WIFI_SSID, WIFI_KEY
@sourceperl
sourceperl / cv_poly.py
Created December 6, 2022 20:08
Fit a polynomial to reflect the Cv curve of a control valve (Cv/position).
#!/usr/bin/env python3
"""Fit a polynomial to reflect the Cv curve of a control valve (Cv/position)."""
import numpy as np
import matplotlib.pyplot as plt
# a list of reference points extracted from the data sheet of the valve
ref_pts_l = [(0, 0), (4, 5), (10, 15), (16, 21), (20, 25), (30, 40), (40, 75),
@sourceperl
sourceperl / net-audit.py
Last active November 17, 2022 17:29
Basic ACL test script tool.
"""
A basic ACL test script tool.
Test under Python 3.10 on Windows 10 Entreprise (64 bits)
home: https://gist.github.com/sourceperl/548b7cfb35629eceee7163e877bb62d7
"""
import logging
import socket
@sourceperl
sourceperl / prometheus_endpoint.py
Created June 11, 2022 19:51
Basic HTTP endpoint for Prometheus
#!/usr/bin/env python3
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from random import randint
class HandleRequests(BaseHTTPRequestHandler):
"""Custom HTTP handler"""
def version_string(self):
return 'self-service here ;-)'