Skip to content

Instantly share code, notes, and snippets.

@exallium
Created September 11, 2012 19:53
Show Gist options
  • Select an option

  • Save exallium/3701561 to your computer and use it in GitHub Desktop.

Select an option

Save exallium/3701561 to your computer and use it in GitHub Desktop.
A neat way to cripple your network
from lxml.html import fromstring
from httplib import HTTPConnection
class Crawler(object):
max_depth = 2
def __init__(self, href=None, depth=0):
if depth == self.max_depth:
return
self.depth = depth
self.href = href or 'www.google.ca'
for x in ['https://', 'http://']:
self.href = self.href.replace(x, '')
self.params = self.href[self.href.index('?'):] if '?' in self.href else ""
if self.params:
self.href = self.href.replace(self.params, '')
self.resource = self.href[self.href.index('/'):] if '/' in self.href else '/index.html'
if self.resource:
self.href = self.href.replace(self.resource, '')
if self.resource == '/': self.resource = '/index.html'
self.run()
def run(self):
conn = HTTPConnection(self.href, 80)
conn.request("GET", "%s%s" % (self.resource, self.params))
res = conn.getresponse()
data = fromstring(res.read())
for link in data.cssselect('a'):
if link.get('href') and link.get('href').startswith('http'):
print link.get('href')
Crawler(link.get('href'), self.depth + 1)
if __name__ == '__main__':
c = Crawler()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment