Skip to content

Instantly share code, notes, and snippets.

@husio
Created September 28, 2009 20:11
Show Gist options
  • Save husio/195807 to your computer and use it in GitHub Desktop.
Save husio/195807 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from twisted.internet import reactor
from twisted.web import client
from twisted.internet.defer import DeferredList
def check_url(url, status_handler):
def _page_fail(reason):
status_handler(url, False)
def _page_ok(data):
status_handler(url, True)
def _handle_page(data):
if factory.status == '200':
return _page_ok(data)
return _page_fail(factory.status)
scheme, host, port, path = client._parse(url)
factory = client.HTTPClientFactory(url, method='HEAD')
reactor.connectTCP(host, port, factory)
factory.deferred.addCallback(_handle_page).addErrback(_page_fail)
return factory.deferred
def printer(url, status):
print status, url
def main():
urls = [
'http://google.pl',
'http://does_not_exist.foo.bar.com',
]
deferreds = [check_url(url, printer) for url in urls]
DeferredList(deferreds).addCallback(lambda x: reactor.stop())
reactor.run()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment