diff --git a/Chapter3-Deferreds/ex4.py b/Chapter3-Deferreds/ex4.py index d9482e8..b6b7b53 100644 --- a/Chapter3-Deferreds/ex4.py +++ b/Chapter3-Deferreds/ex4.py @@ -1,10 +1,17 @@ +# Errata note: when TNPE 2ed was first printed, Twisted 12.0.0 was the latest +# version of Twisted. Since then, passing bare strings to the errback method +# was deprecated in Twisted 12.3.0. A failure or Exception must now be +# passed instead. + from twisted.internet import reactor, defer + class HeadlineRetriever(object): + def processHeadline(self, headline): if len(headline) > 50: self.d.errback( - "The headline ``%s'' is too long!" % (headline,)) + ValueError("The headline ``%s'' is too long!" % (headline,))) else: self.d.callback(headline) @@ -17,10 +24,12 @@ def getHeadline(self, input): self.d.addCallback(self._toHTML) return self.d + def printData(result): print result reactor.stop() + def printError(failure): print failure reactor.stop() diff --git a/Chapter4-Web-Servers/ex2-requesthandler.py b/Chapter4-Web-Servers/ex2-requesthandler.py index 504580c..a49663c 100644 --- a/Chapter4-Web-Servers/ex2-requesthandler.py +++ b/Chapter4-Web-Servers/ex2-requesthandler.py @@ -9,7 +9,7 @@ class MyRequestHandler(http.Request): def process(self): self.setHeader('Content-Type', 'text/html') - if self.resources.has_key(self.path): + if self.path in self.resources: self.write(self.resources[self.path]) else: self.setResponseCode(http.NOT_FOUND) diff --git a/Chapter5-Web-Clients/ex1-print_resource.py b/Chapter5-Web-Clients/ex1-print_resource.py index 039a102..2e92f0a 100644 --- a/Chapter5-Web-Clients/ex1-print_resource.py +++ b/Chapter5-Web-Clients/ex1-print_resource.py @@ -12,7 +12,7 @@ def stop(result): reactor.stop() if len(sys.argv) != 2: - print >>sys.stderr, "Usage: python print_resource.py " + printError("Usage: python print_resource.py ") exit(1) d = getPage(sys.argv[1]) diff --git a/Chapter5-Web-Clients/ex2-download_resource.py b/Chapter5-Web-Clients/ex2-download_resource.py index 77a200e..a05eeac 100644 --- a/Chapter5-Web-Clients/ex2-download_resource.py +++ b/Chapter5-Web-Clients/ex2-download_resource.py @@ -9,7 +9,7 @@ def stop(result): reactor.stop() if len(sys.argv) != 3: - print >>sys.stderr, "Usage: python download_resource.py " + printError("Usage: python download_resource.py ") exit(1) d = downloadPage(sys.argv[1], sys.argv[2]) diff --git a/Chapter5-Web-Clients/ex3-agent_print_resource.py b/Chapter5-Web-Clients/ex3-agent_print_resource.py index 02b046b..d340ade 100644 --- a/Chapter5-Web-Clients/ex3-agent_print_resource.py +++ b/Chapter5-Web-Clients/ex3-agent_print_resource.py @@ -27,7 +27,7 @@ def stop(result): reactor.stop() if len(sys.argv) != 2: - print >>sys.stderr, "Usage: python agent_print_resource.py URL" + printError("Usage: python agent_print_resource.py URL") exit(1) agent = Agent(reactor) diff --git a/Chapter5-Web-Clients/ex4-print_metadata.py b/Chapter5-Web-Clients/ex4-print_metadata.py index 1900fbf..6319f39 100644 --- a/Chapter5-Web-Clients/ex4-print_metadata.py +++ b/Chapter5-Web-Clients/ex4-print_metadata.py @@ -19,7 +19,7 @@ def stop(result): reactor.stop() if len(sys.argv) != 2: - print >>sys.stderr, "Usage: python print_metadata.py URL" + printError("Usage: python print_metadata.py URL") exit(1) agent = Agent(reactor) diff --git a/Chapter5-Web-Clients/ex5-post_data.py b/Chapter5-Web-Clients/ex5-post_data.py index fe8b498..d5edd4b 100644 --- a/Chapter5-Web-Clients/ex5-post_data.py +++ b/Chapter5-Web-Clients/ex5-post_data.py @@ -46,7 +46,7 @@ def stop(result): reactor.stop() if len(sys.argv) != 3: - print >>sys.stderr, "Usage: python post_resource.py URL 'POST DATA'" + printError("Usage: python post_resource.py URL 'POST DATA'") exit(1) agent = Agent(reactor)