Skip to content

Instantly share code, notes, and snippets.

@gnrfan
Last active December 15, 2015 07:49
Show Gist options
  • Select an option

  • Save gnrfan/5226322 to your computer and use it in GitHub Desktop.

Select an option

Save gnrfan/5226322 to your computer and use it in GitHub Desktop.
A gevent demo making DNS queries both in a blocking and non-blocking way.

gevent demo

Non-blocking IO using co-routines in Python.

Installing gevent under OSX using MacPorts

sudo port install libevent
virtualenv --no-site-packages env
source env/bin/activate
pip install cython
pip install greenlet
CFLAGS="-I /opt/local/include -L /opt/local/lib" pip install gevent

Running the benchmark

$ time python blocking.py google-domains.txt

$ time python non_blocking.py google-domains.txt
import sys
import socket
if len(sys.argv) != 2:
print "Usage: %s /path/to/domains-file.txt" % (sys.argv[0])
sys.exit(1)
filepath = sys.argv[1]
ip_list = []
domains = [d.strip() for d in open(filepath)]
for d in domains:
try:
ip_addr = socket.gethostbyname(d)
if ip_addr not in ip_list:
ip_list.append(ip_addr)
except socket.gaierror:
pass
print ip_list
1e100.net
466453.com
AdSense.com
AdWords.com
Android.com
DoubleClick.com
FeedBurner.com
Picasa.com
Sketchup.com
blogger.com
blogspot.com
chromium.orgg
froogle.com
g.co
ggpht.com
gogle.com
gogole.com
goo.gl
googel.com
googil.com
google.net
google.org
googleapis.com
googleapps.com
googlebot.com
googleratings.com
googlr.com
goolge.com
gooogle.com
gplus.to
gstatic.com
gtempaccount.com
igoogle.com
keyhole.com
like.com
panoramio.com
webcache.googleusercontent.com
www.g.cn
www.google.ac
www.google.ad
www.google.al
www.google.am
www.google.as
www.google.at
www.google.az
www.google.be
www.google.bf
www.google.bg
www.google.bi
www.google.bj
www.google.bs
www.google.ca
www.google.cat
www.google.ch
www.google.ci
www.google.cl
www.google.cm
www.google.cn
www.google.co.ao
www.google.co.bw
www.google.co.ck
www.google.co.cr
www.google.co.idg
www.google.co.ilg
www.google.co.ing
www.google.co.jpg
www.google.co.keg
www.google.co.krg
www.google.co.lsg
www.google.co.mag
www.google.co.mzg
www.google.co.nzg
www.google.co.thg
www.google.co.tzg
www.google.co.ugg
www.google.co.ukg
www.google.co.uzg
www.google.co.veg
www.google.co.za
www.google.co.zm
www.google.co.zw
www.google.com.af
www.google.com.ai
www.google.com.ar
www.google.com.au
www.google.com.bd
www.google.com.bh
www.google.com.bn
www.google.com.bo
www.google.com.br
www.google.com.by
www.google.com.bz
www.google.com.co
www.google.com.cu
www.google.com.cy
www.google.com.do
www.google.com.ec
www.google.com.eg
www.google.com.et
www.google.com.fj
www.google.com.gh
www.google.com.gi
www.google.com.gtg
www.google.com.hkg
www.google.com.jmg
www.google.com.kh
www.google.com.khg
www.google.com.kwg
www.google.com.lbg
www.google.com.lcg
www.google.com.lyg
www.google.com.mtg
www.google.com.mxg
www.google.com.myg
www.google.com.nag
www.google.com.nfg
www.google.com.ngg
www.google.com.nig
www.google.com.npg
www.google.com.omg
www.google.com.pag
www.google.com.peg
www.google.com.phg
www.google.com.pkg
www.google.com.prg
www.google.com.pyg
www.google.com.qag
www.google.com.sag
www.google.com.sbg
www.google.com.sgg
www.google.com.slg
www.google.com.svg
www.google.com.tjg
www.google.com.tng
www.google.com.trg
www.google.com.twg
www.google.com.uag
www.google.com.uyg
www.google.com.vng
www.google.cv
www.google.cz
www.google.de
www.google.dj
www.google.dk
www.google.dm
www.google.dz
www.google.ee
www.google.es
www.google.fi
www.google.fr
www.google.ga
www.google.ge
www.google.gf
www.google.gg
www.google.glg
www.google.gmg
www.google.gpg
www.google.grg
www.google.gyg
www.google.hng
www.google.hrg
www.google.htg
www.google.hug
www.google.ieg
www.google.iqg
www.google.isg
www.google.itg
www.google.jeg
www.google.jog
www.google.kgg
www.google.kig
www.google.kzg
www.google.lag
www.google.lig
www.google.lkg
www.google.ltg
www.google.lug
www.google.lvg
www.google.mdg
www.google.meg
www.google.mgg
www.google.mkg
www.google.mlg
www.google.mng
www.google.msg
www.google.mug
www.google.mvg
www.google.mwg
www.google.neg
www.google.nlg
www.google.nog
www.google.nrg
www.google.nug
www.google.plg
www.google.png
www.google.psg
www.google.ptg
www.google.rog
www.google.rsg
www.google.rug
www.google.rwg
www.google.scg
www.google.seg
www.google.sig
www.google.skg
www.google.smg
www.google.sng
www.google.sog
www.google.tdg
www.google.tgg
www.google.tkg
www.google.tlg
www.google.tmg
www.google.tog
www.google.usg
www.google.vu
www.google.ws
youtu.be
youtube.com
import sys
import socket
import gevent
from gevent import socket
if len(sys.argv) != 2:
print "Usage: %s /path/to/domains-file.txt" % (sys.argv[0])
sys.exit(1)
filepath = sys.argv[1]
ip_list = []
domains = [d.strip() for d in open(filepath)]
def dns_lookup(domain):
try:
return socket.gethostbyname(domain)
except gevent.dns.DNSError:
return None
jobs = [gevent.spawn(dns_lookup, d) for d in domains]
gevent.joinall(jobs, timeout=2)
for j in jobs:
if j.value is None:
continue
if not j.value in ip_list:
ip_list.append(j.value)
print ip_list
$ time python non_blocking.py google-domains.txt
['173.194.75.104', '74.125.26.147', '173.194.76.51', '208.117.253.244', '70.32.146.212', '208.117.253.25', '173.194.75.105', '66.45.58.22', '173.194.76.191', '173.194.68.191', '74.125.26.104', '208.117.253.151', '173.194.75.106', '74.125.26.103', '208.117.253.214', '74.125.26.105', '173.194.75.99', '74.125.26.106', '216.239.32.27', '173.194.74.118', '74.125.26.99', '94.73.144.190', '173.194.68.120', '74.125.54.206', '208.117.253.183', '208.117.253.86', '173.194.74.132', '173.194.74.160', '74.125.26.94', '173.194.75.94', '173.194.68.160', '208.117.253.59', '208.117.253.89']
real 0m2.480s
user 0m0.100s
sys 0m0.064s
$ time python blocking.py google-domains.txt
['173.194.75.104', '173.194.75.103', '173.194.74.51', '74.125.26.105', '70.32.146.212', '208.117.253.86', '66.45.58.22', '173.194.68.191', '173.194.74.191', '74.125.26.147', '208.117.253.59', '173.194.75.99', '208.117.253.185', '74.125.26.103', '173.194.75.105', '216.239.32.27', '173.194.73.104', '173.194.74.118', '74.125.26.99', '173.194.73.106', '94.73.144.190', '173.194.68.120', '173.194.73.147', '74.125.54.206', '208.117.253.183', '208.117.253.87', '173.194.68.132', '173.194.74.160', '74.125.26.94', '173.194.73.94', '173.194.75.94', '173.194.76.160', '208.117.253.53', '208.117.253.153']
real 0m34.046s
user 0m0.061s
sys 0m0.062s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment