[Twisted-Python] Difficulty with twisted.web.client.getPage function
Dave Gray
dgray at omniti.com
Fri Jun 10 17:25:49 EDT 2005
Alan Jern wrote:
> Hey, I'm new to Twisted and I've run into a problem that I can't solve.
>
> I'm trying to write a simple Resource class that fetches a page from a
> specified URL and returns it to the browser. I created a class called
> MyResource in a .rpy file with the following two member functions:
>
> def print_page (self, html):
> self.rq.write(html)
> self.rq.finish()
>
> def render_GET (self, request):
> self.rq = request
> target = escape(request.args['target'][0])
> getPage(target).addCallback(self.print_page)
> return server.NOT_DONE_YET
>
> When I run this script and send a request, the print_page function is
> invoked but the variable 'html' has nothing in it. According to the
> API and the examples I saw on the HOWTO section, this looks like it
> ought to work. Clearly I'm missing something.
You will probably get more enthusiastic responses to questions like this
on twisted-web (cc'd), FYI
That said, this works for me:
---fetchpage.tac---
# run me with twistd -noy fetchpage.tac
from twisted.web import server
from twisted.web.resource import Resource
from twisted.web.client import getPage
from cgi import escape
class Foo(Resource):
isLeaf=True
def print_page (self, html):
self.rq.write(html)
self.rq.finish()
def render_GET (self, request):
self.rq = request
target = escape(request.args['target'][0])
getPage(target).addCallback(self.print_page)
return server.NOT_DONE_YET
resource = Foo()
site = server.Site(resource)
from twisted.application import service, internet
application = service.Application("PageFetcher")
internet.TCPServer(9000, site).setServiceParent(application)
# and then from your command prompt:
> telnet localhost 9000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /?target=http://google.com/ HTTP/1.0
Host: localhost
HTTP/1.0 200 OK
Date: Fri, 10 Jun 2005 21:20:49 GMT
Content-type: text/html
Server: TwistedWeb/2.0.0
<html><head><meta http-equiv="content-type" content="text/html;
charset=ISO-8859-1"><title>Google</title><style><!--
etc etc
More information about the Twisted-Python
mailing list