Skip to content

Instantly share code, notes, and snippets.

@saghul
saghul / test_sip_auth.py
Created February 11, 2011 09:15
Calculate response hash to HTTP digest authentication challenge
from hashlib import md5
# Calculate response hash to authentication challenge
def calc_auth_response(username, password, realm, nonce, digest_uri, method):
ha1 = md5('%s:%s:%s' % (username, realm, password)).hexdigest()
ha2 = md5('%s:%s' % (method, digest_uri)).hexdigest()
response = md5('%s:%s:%s' % (ha1, nonce, ha2)).hexdigest()
return response
@saghul
saghul / weaksingleton.py
Created March 7, 2011 22:22
WeakSingleton
# Derived from Singleton class by Dan Pascu on python-application package
from new import instancemethod
from weakref import WeakValueDictionary
from application.python.decorator import preserve_signature
class WeakSingleton(type):
"""
@saghul
saghul / test_tls.php
Created March 22, 2011 10:32
Connect to MediaProxy dispatcher and get sessions using TLS
<?php
$host ='1.2.3.4';
$port = 25061;
$timeout = 10;
$cert = '/tmp/mediaproxy.pem';
$context = stream_context_create(array('ssl'=>array('local_cert'=> $cert,)));
$fp = stream_socket_client('tcp://'.$host.':'.$port, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context);
if (!$fp) {
echo "ERROR: $errno - $errstr\n";
} else {
@saghul
saghul / gist:936469
Created April 22, 2011 11:23
lighttpd + PHP-FPM
server.modules += ( "mod_fastcgi" )
fastcgi.server = ( ".php" =>
( "localhost" =>
(
"host" => "127.0.0.1",
"port" => "9000"
)
)
)
@saghul
saghul / twisted_sqlobj.py
Created May 26, 2011 21:13
Serialize SQLite operations with SQLObject and Twisted
# coding=utf8
# Copyright (C) 2011 Saúl Ibarra Corretgé <[email protected]>
#
__all__ = ['Database', 'DatabaseError']
from threading import Thread
from sqlobject import connectionForURI, sqlhub, SQLObject, StringCol
from twisted.internet import reactor
from twisted.internet.threads import deferToThreadPool
@saghul
saghul / twisted_sqlobj.py
Created May 26, 2011 21:15
Creating a Twisted ThreadPool
pool = ThreadPool(minthreads=1, maxthreads=1, name='db-ops')
pool.start()
reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
@saghul
saghul / twisted_sqlobj.py
Created May 26, 2011 21:15
Decorator to run the decorated function in a ThreadPool and return a deferred
def run_in_db_thread(func):
"""Decorator to run DB queries in Twisted's thread pool"""
def wrapper(*args, **kw):
return deferToThreadPool(reactor, pool, func, *args, **kw)
return wrapper
@saghul
saghul / gist:1008874
Created June 5, 2011 11:11
Default function arguments (mutable type)
In [1]: class Foo(object):
...: def __init__(self, bar=[]):
...: self.bar = bar
...:
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
@saghul
saghul / gist:1008875
Created June 5, 2011 11:13
Default function arguments (non-mutable type)
In [1]: class Foo(object):
...: def __init__(self, bar=5):
...: self.bar = bar
...:
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()
@saghul
saghul / gist:1008879
Created June 5, 2011 11:19
Default function arguments (mutable type, OK)
In [1]: class Foo(object):
...: def __init__(self, bar=None):
...: self.bar = bar or []
...:
...:
In [2]: f1 = Foo()
In [3]: f2 = Foo()