Skip to content

Instantly share code, notes, and snippets.

View zielmicha's full-sized avatar

Michał Zieliński zielmicha

View GitHub Profile
@zielmicha
zielmicha / verifiedhttps.py
Created February 2, 2013 13:19
Verifies certificates when connecting using HTTPS. Requires Python >=3.2. Based on http://thejosephturner.com/blog/2011/03/19/https-certificate-verification-in-python-with-urllib2/.
# Verifies certificates when connecting using HTTPS.
# Needs cacert.txt (cat /usr/share/ca-certificates/mozilla/* > cacert.txt).
import http.client
import socket
import ssl
class VerifiedHTTPSConnection(http.client.HTTPSConnection):
def connect(self):
sock = socket.create_connection((self.host, self.port), self.timeout)
if self._tunnel_host:
@zielmicha
zielmicha / linum.py
Last active December 13, 2015 20:08
Make LOC history of a git repo and display pretty graph.
# Make LOC history of a git repo and display pretty graph.
#
# Usage:
# cd ~/myrepo
# python linum.py ~/mylinecountingscript.sh
#
# Counting script should count LOC in current directory. Example:
# wc --lines $(find -name '*.py')
#
# Requires Python Matplotlib.
@zielmicha
zielmicha / tempwatch.py
Created August 1, 2013 16:19
Suspend if your CPU is getting too hot.
#!/usr/bin/env python
import sensors
import subprocess
import time
def emergency():
subprocess.check_call(['sudo', 'pm-suspend'])
def watch():
while True:
@zielmicha
zielmicha / snd.py
Created August 10, 2013 21:06
Emit sounds during typing. sudo python snd.py (list devices) sudo python snd.py /dev/input/<your keyboard> Requires: pip install evdev
from __future__ import division
from math import sin
import math
import sys
import wave
import struct
import subprocess
import os
RATE = 44000
snd_i = 0
@zielmicha
zielmicha / makefile.py
Created September 2, 2013 16:27
Makefile generating "library". Very lightweight alternative to CMake, autotools etc.
import pipes
import shlex
def shellquote(s):
return pipes.quote(s) if s else "''"
def reext(thing, src, dst):
if isinstance(thing, list):
return map(lambda a: reext(a, src, dst), thing)
@zielmicha
zielmicha / hashit.py
Created December 2, 2013 14:39
Generate password for website from master one
import getpass, hashlib
domain = raw_input('domain: ')
assert not domain.startswith('www.')
pas = getpass.getpass()
pas1 = getpass.getpass()
assert pas == pas1
print hashlib.sha1(domain + pas).hexdigest()[:16]
@zielmicha
zielmicha / destest.nim
Created December 4, 2013 20:47
Destructors don't work on objects allocated by new :(
type
TThing = object
a: int
PThing = ref TThing
proc destruct1(instance: var TThing) {.destructor.} =
echo "destory TThing number ", instance.a
block:
var a: TThing = TThing(a: 2)
# Usage: ifconfig | python ifco.py
import sys
import socket
readline = sys.stdin.readline
def ip2num(a):
return int(socket.inet_aton(a).encode('hex'), 16)
def num2ip(i):
return socket.inet_ntoa(hex(i)[2:].decode('hex'))
@zielmicha
zielmicha / unmkbootimg.py
Last active August 29, 2015 13:57
Unpack Android's bootimg into kernel image and ramdisk
import struct
import sys
import gzip
fn = sys.argv[1]
f = open(fn)
if f.read(8) != 'ANDROID!':
sys.exit('Not a mkbootimg file.')
_header = struct.unpack('<' + 'I' * 10, f.read(10 * 4))
import pygame
import argparse
import time
parser = argparse.ArgumentParser(description='Display raw image (typically a framebuffer).')
parser.add_argument('file')
parser.add_argument('width', type=int)
parser.add_argument('height', type=int)
parser.add_argument('--format', default='RGBX')