Chapter 5. Web Clients
This chapter will talk about the HTTP client side of Twisted Web,
starting with quick web resource retrieval for one-off applications and
ending with the Agent API for developing
flexible web clients.
Basic HTTP Resource Retrieval
Twisted has several high-level convenience classes for quick one-off resource retrieval.
Printing a Web Resource
twisted.web.client.getPage
asynchronously retrieves a resource at a given URL. It returns a Deferred, which fires its callback with the
resource as a string. Example 5-1
demonstrates the use of getPage; it
retrieves and prints the resource at the user-supplied URL.
fromtwisted.internetimportreactorfromtwisted.web.clientimportgetPageimportsysdefprintPage(result):resultdefprintError(failure):>>sys.stderr,failuredefstop(result):reactor.stop()iflen(sys.argv)!=2:>>sys.stderr,"Usage: python print_resource.py <URL>"exit(1)d=getPage(sys.argv[1])d.addCallbacks(printPage,printError)d.addBoth(stop)reactor.run()
We can test this script with:
python print_resource.py http://www.google.com
which will print the contents of Google’s home page to the screen.
An invalid URL will produce something like the following:
$ python print_resource.py http://notvalid.foo [Failure instance: Traceback (failure with no frames): <class 'twisted.internet.error.DNSLookupError'>: DNS lookup failed: address 'notvalid.foo' not found: [Errno 8] nodename nor servname provided, or not known. ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access