Skip to content

Instantly share code, notes, and snippets.

@hankchan
hankchan / htmlabbrev.py
Created June 16, 2012 00:45 — forked from dcrosta/htmlabbrev.py
truncates HTML without screwing up tag nesting
import re
from HTMLParser import HTMLParser
whitespace = re.compile('(\w+)')
class HTMLAbbrev(HTMLParser):
def __init__(self, maxlength, *args, **kwargs):
HTMLParser.__init__(self, *args, **kwargs)
self.stack = []
@hankchan
hankchan / upgrade_example.py
Created April 11, 2012 07:30 — forked from denik/upgrade_example.py
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)
@hankchan
hankchan / gevent-multiprocess.py
Created April 11, 2012 07:28 — forked from denik/gevent-multiprocess.py
gevent-multiprocess
import sys
from gevent import server
from gevent.baseserver import _tcp_listener
from gevent.monkey import patch_all; patch_all()
from multiprocessing import Process, current_process, cpu_count
def note(format, *args):
sys.stderr.write('[%s]\t%s\n' % (current_process().name, format%args))
@hankchan
hankchan / ServerRack.py
Created April 9, 2012 02:42 — forked from denik/ServerRack.py
class for managing multiple servers in gevent
# Class for managing multiple servers or anything with start() and stop() methods
class ServerRack(object):
def __init__(self, servers):
self.servers = servers
def start(self):
started = []
try: