Skip to content

Instantly share code, notes, and snippets.

View fiorix's full-sized avatar

Alexandre Fiori fiorix

View GitHub Profile
#!/usr/bin/env python
# coding: utf-8
# How Twisted's inlineCallbacks work?
# If you use it but don't know exactly what it does,
# and didn't understand the code shipped with Twisted,
# keep reading.
import types, functools
from twisted.python import failure
from twisted.web.client import getPage
def checkQuota(method):
@defer.inlineCallbacks
@functools.wraps(method)
def wrapper(self, *args, **kwargs):
key = "ip:%s" % self.request.remote_ip
try:
n = yield self.redis.incr(key)
except Exception, e:
log.msg("Redis failed to incr('%s'): %s" % (key, str(e)))
@fiorix
fiorix / tcp-proxy.py
Created February 21, 2012 21:12
twisted tcp proxy
#!/usr/bin/env python
# coding: utf-8
# http://musta.sh/2012-03-04/twisted-tcp-proxy.html
import sys
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.python import log
@fiorix
fiorix / webtail.py
Created February 27, 2012 00:16
tail -F and broadcast to all of your browsers via sse
#!/usr/bin/env python
# coding: utf-8
#
# start the server:
# python webtail.py /var/log/syslog
#
# point all your browsers to:
# http://localhost:8888
@fiorix
fiorix / txredisapi_inet_vs_unix.py
Created March 16, 2012 12:59
compare txredisapi's inet vs unix sockets
#!/usr/bin/env python
# coding: utf-8
#
# Compare txredisapi's inet vs unix sockets.
# Don't forget to enable redis' unix socket prior to executing this.
import time
import txredisapi as redis
from twisted.internet import defer
@fiorix
fiorix / akinator.py
Created July 20, 2012 19:49
Interactive command line crawler to akinator.com
#!/usr/bin/env python
# coding: utf-8
#
# Interactive command line crawler to akinator.com
# http://musta.sh/2012-07-20/twisting-python-and-freeswitch.html
import re
import sys
from twisted.internet import defer
@fiorix
fiorix / test.py
Created August 30, 2012 03:03
twisted memcache client on cyclone web server
# coding: utf-8
# twistd -n cyclone -r test.Application
import cyclone.web
from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.protocols.memcache import MemCacheProtocol
@fiorix
fiorix / cities.py
Created January 22, 2013 07:27
A list of all cities in the world.
#!/usr/bin/env python
# coding: utf-8
#
# The World Gazetteer provides a downloadable file that contains a list
# of all cities, towns, administrative divisions and agglomerations with
# their population, their English name parent country.
#
# Article: http://answers.google.com/answers/threadview/id/774429.html
# Download: http://www.world-gazetteer.com/dataen.zip
@fiorix
fiorix / gist:9664255
Created March 20, 2014 13:55
Go multicast example
package main
import (
"encoding/hex"
"log"
"net"
"time"
)
const (
@fiorix
fiorix / gist:9927296
Created April 2, 2014 03:03
Reading sequential pcap payload in Go
func pcapReader(filename string) (io.ReadCloser, error) {
pr, pw, err := os.Pipe()
if err != nil {
return nil, err
}
handle, err := pcap.OpenOffline(filename)
if err != nil {
return nil, err
}