Skip to content

Instantly share code, notes, and snippets.

@tomazas
tomazas / http_multithreaded_python2.py
Created June 29, 2023 13:27
Simple Python 2 HTTP multi-threaded server
# Python 2 HTTP GET/POST multi-threaded server
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from SocketServer import ThreadingMixIn
import threading
hostName = "0.0.0.0"
serverPort = 8080
class Handler(BaseHTTPRequestHandler):
@tomazas
tomazas / duplicate_class.py
Last active May 4, 2023 11:39
Detect duplicate Java classes after scanning all JAR/WAR files in provided directory
# Script detects duplicate Java classes after scanning all JAR/WAR files in provided directory.
# usage: python script <dir>
import zipfile
import sys
import os
if len(sys.argv) < 2:
print("usage: python %s <dir>"%(sys.argv[0]))
sys.exit(1)
@tomazas
tomazas / http_ssl_server.py
Last active January 25, 2022 14:29
Python 3 multi-threaded HTTPS server implementing GET & POST on port 443
# Python 3 multi-threaded HTTPS server implementing GET & POST on port 443
# Tested using Python 3.10.1
# can be used with a generated self-signed certificate using bash command:
# openssl req -x509 -nodes -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365
# run using below run.sh bash script:
'''
#!/bin/bash
nohup /usr/bin/python -u http_ssl_server.py > server.log 2>&1 &
@tomazas
tomazas / http_server.py
Last active January 25, 2022 09:43
Python 3 multi-threaded HTTP server implementing GET & POST on port 8080
# Python 3 multi-threaded HTTP server implementing GET & POST on port 8080
# Tested using Python 3.10.1
# run using below run.sh bash script:
'''
#!/bin/bash
nohup /usr/bin/python -u http_server.py > server.log 2>&1 &
echo $! > pid
'''
# python 2 HTTP GET single-threaded server
# input: HTTP XML payload loded from payload.txt file
#
import SimpleHTTPServer
import SocketServer
hostName = "localhost"
serverPort = 8000
payload = ""
@tomazas
tomazas / python_wave_sample.py
Created January 27, 2021 10:51
Generate 440Hz sine signal-audio with Python wave library
import wave, struct, math, random
sampleRate = 44100 # samples/sec (Fs)
duration = 10 # seconds
frequency = 440 # hertz
obj = wave.open('sound.wav','w')
obj.setnchannels(1) # mono
obj.setsampwidth(2) # 16-bit
obj.setframerate(sampleRate)
@tomazas
tomazas / java_jar_version_checker.py
Created September 16, 2020 07:26
Java JAR compiler version checker
# script detects Java compiler version for a provided input JAR filename
# usage: python script <input.jar>
import zipfile
import sys
import os
import shutil
import struct
if len(sys.argv) < 2:
@tomazas
tomazas / digispark_revival.ino
Created June 30, 2020 19:30
Revive Digispark ATTiny85 sketch
// Fixes the Digispark board brown out (USB re-enumeration) issue
//
// 1. Install drivers: https://github.com/digistump/DigistumpArduino/releases/download/1.6.7/Digistump.Drivers.zip
// 2. Install Digispark boards: https://raw.githubusercontent.com/digistump/arduino-boards-index/master/package_digistump_index.json
// 3. Choose Tool > Board > Digispark (Default - 16.5mhz)
// 4. Upload this sketch
// 5. Plugin the board in USB
void setup() {
// initialize the digital pin as an output.
@tomazas
tomazas / arduino_bandgap_vcc.ino
Last active July 31, 2019 20:54
Example of measuring atmega VCC using the bandgap method (no external hardware or voltage divider needed).
/*
Example of measuring atmega VCC using the bandgap method (no external hardware or voltage divider needed).
Based on: https://jeelabs.org/2012/05/04/measuring-vcc-via-the-bandgap/
*/
// Arduino Nano / Atmega328 tested
static int vccRead () {
ADMUX = bit(REFS0) | 14; // use Vref=VCC and measure internal bandgap channel 1.1V
delayMicroseconds(250); // wait for bandgap to stabilize
@tomazas
tomazas / luhn.py
Last active April 7, 2017 08:49
Luhn python implementation
# Luhn python implementation: http://www.ee.unb.ca/tervo/ee4253/luhn.shtml
def luhn(input):
sum = 0
for i,c in enumerate(input):
num = (2-(i % 2)) * int(c)
sum += int(num/10) + (num % 10)
#print sum
return ((sum % 10) == 0)