Skip to content

Instantly share code, notes, and snippets.

@progrium
progrium / urlfetch_intercept.py
Created February 9, 2011 06:15
wsgi_intercept for app engine's urlfetch
""" Monkey patch to hit wsgi apps when using urlfetch
This will monkey patch App Engine's urlfetch.fetch with a fetch that hits a local
wsgi app registered with add_intercept. This module is inspired by and borrows
code from the wsgi-intercept project, which doesn't work with App Engine.
This is intended only for the local SDK environment for unit tests.
Usage:
""" Cruster -- distributed group membership system
Cruster provides distributed group membership for easily building clustered
applications with gevent. Using Cruster in your app, you just provide the IP
of another node in the cluster and it will receive the IPs of all nodes in
the cluster. When a node joins or drops from the cluster, all other nodes find
out immediately.
The roster is managed by a leader. When you create a cluster, you tell the
first node it is the leader (by simply pointing it to its own IP). As you
"""A line protocol generator
Usage:
import socket
sock = socket.create_connection((host, port))
for line in line_protocol(sock):
print line
sock.send("ack!")
@progrium
progrium / gist:956006
Created May 4, 2011 20:50
Greenlet exception handling in gevent
import gevent
import gevent.pool
class GroupWithExceptionCatching(gevent.pool.Group):
def __init__(self, *args):
super(GroupWithExceptionCatching, self).__init__(*args)
self._error_handlers = {}
def _wrap_errors(self, func):
"""Wrap a callable for triggering error handlers
import asyncore, socket, sys
class MyClient(asyncore.dispatcher):
def __init__(self, host, port, name):
asyncore.dispatcher.__init__(self)
self.name = name
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect( (host, port) )
print 'Started to connect.'
def getPage(url):
d = Deferred()
# here it would set up an http client programmed to call
# d.callback(body) when page is fully received...
return d
def handleBody(body):
print body
function repeat(times:int, loop:Function) {
for (i=0;i<times;i++){
loop.call();
}
}
repeat(10, function() {
// stuff
})
>>> h.heap()
Partition of a set of 3149666 objects. Total size = 432576704 bytes.
Index Count % Size % Cumulative % Kind (class / dict of class)
0 301452 10 110323872 26 110323872 26 dict (no owner)
1 299175 9 88555800 20 198879672 46 realtime.utils.attrdict
2 299176 9 83769280 19 282648952 65 dict of realtime.utils.dispatcher.CallbackDispatcher
3 669660 21 45685424 11 328334376 76 str
4 599730 19 43301008 10 371635384 86 list
5 339907 11 24939144 6 396574528 92 tuple
6 299176 9 19147264 4 415721792 96 realtime.utils.dispatcher.CallbackDispatcher
for scope in path_scopes:
filter_constraints = cgi.parse_qs(scope.params.get('params', ''))
filter_keys = set(request.args.keys()) & set(filter_constraints.keys())
def arg_satisfies_filter(f):
return set(request.args[f]).issubset(set(filter_constraints[f]))
if all(arg_satisfies_filter(f) for f in filter_keys):
# if for every parameter, the values are a subset of allowed params, we good!
return True
@progrium
progrium / upgrade_example.py
Created August 17, 2011 17:39
Upgradable WSGI server websocket example
import gevent.pywsgi
from websocket import WebSocketUpgrader
class UpgradableWSGIHandler(gevent.pywsgi.WSGIHandler):
def handle_one_response(self):
connection_header = self.environ.get('HTTP_CONNECTION', '').lower()
if connection_header == 'upgrade' and self.server.upgrade_handler:
upgrade_header = self.environ.get('HTTP_UPGRADE', '').lower()
handler = self.server.upgrade_handler(upgrade_header, self.environ)