Setting server.Site.protocol stops the site factory resource class from renderin
I have the code below that works fine. If I uncomment the 3rd to last line so that the site.protocol uses the ServerProtocol class, then the render_GET() method does not get called and the browser waits for a response. I understand why this is happening but don't know how to fix it. I need to override the connectionMade() method for the site factory inorder to verify the connection. Can someone explain how I override connectionMade() correctly? Any help appreciated. Cheers Steve import os, sys from twisted.web import http, static, server, resource from twisted.internet.protocol import Protocol from twisted.internet import reactor class Simple(resource.Resource): def render_GET(self, request): sess = request.getSession() request.write("<html>") request.write("Hi there") request.write("</html>") request.finish() return server.NOT_DONE_YET class ServerProtocol(Protocol): def connectionMade(self): print "[connectionMade] entering function....." os.chdir('D:\\web') root = Simple() root.putChild('index.html', Simple()) site = server.Site(root) #site.protocol = ServerProtocol reactor.listenTCP(80, site) reactor.run() _________________________________________________________________ Shop til you drop at XtraMSN Shopping http://shopping.xtramsn.co.nz/home/
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Feb 28, 2007, at 7:52 PM, Steve doh wrote:
I have the code below that works fine. If I uncomment the 3rd to last line so that the site.protocol uses the ServerProtocol class, then the render_GET() method does not get called and the browser waits for a response. I understand why this is happening but don't know how to fix it. I need to override the connectionMade() method for the site factory inorder to verify the connection. Can someone explain how I override connectionMade() correctly? Any help appreciated. Cheers Steve
twisted.internet.protocol.Protocol doesn't know anything about HTTP, and it doesn't know anything about the resource hierarchy and HTTP methods. server.Site.protocol defaults to twisted.web.http.HTTPChannel, which does know what to do when faced with HTTP, so subclass that instead. And don't forget to call the superclass's connectionMade or timeouts won't work properly. - -David http://dreid.org "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Darwin) iD8DBQFF5u0CrsrO6aeULcgRAsQtAJwLjqQuv4AkoapqWKO/0ap/N/YGKgCfZz1o KvS9cz9xZdGaxUW96buX+b8= =Zlqp -----END PGP SIGNATURE-----
Thanks David - it's now working! One minor error I get when I add a super call as: class ServerProtocol(http.HTTPChannel): def connectionMade(self): print "[connectionMade] entering function....." super(ServerProtocol, self).connectionMade() this generates: File "test.py", line 20, in connectionMade super(ServerProtocol, self).connectionMade() exceptions.TypeError: super() argument 1 must be type, not classobj Any ideas? Cheers Steve
From: David Reid
Reply-To: Twisted Web World To: Twisted Web World Subject: Re: [Twisted-web] Setting server.Site.protocol stops the site factoryresource class from renderin Date: Thu, 1 Mar 2007 07:10:51 -0800 -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
On Feb 28, 2007, at 7:52 PM, Steve doh wrote:
I have the code below that works fine. If I uncomment the 3rd to last line so that the site.protocol uses the ServerProtocol class, then the render_GET() method does not get called and the browser waits for a response. I understand why this is happening but don't know how to fix it. I need to override the connectionMade() method for the site factory inorder to verify the connection. Can someone explain how I override connectionMade() correctly? Any help appreciated. Cheers Steve
twisted.internet.protocol.Protocol doesn't know anything about HTTP, and it doesn't know anything about the resource hierarchy and HTTP methods. server.Site.protocol defaults to twisted.web.http.HTTPChannel, which does know what to do when faced with HTTP, so subclass that instead. And don't forget to call the superclass's connectionMade or timeouts won't work properly.
- -David http://dreid.org
_________________________________________________________________ Discover fun and games at @ http://xtramsn.co.nz/kids
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Mar 1, 2007, at 2:08 PM, Steve doh wrote:
argument 1 must be type, not classobj HTTPChannel is an oldstyle class.
Use http.HTTPChannel.connectionMade(self) - -David http://dreid.org "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Darwin) iD8DBQFF5133rsrO6aeULcgRAtZcAJ4l6yYPo3a1e8CuKtqZYhDAzSP/lQCfVavE 7+/9dAitgQlkYyMhxYNeXIM= =fka/ -----END PGP SIGNATURE-----
Hi, In the code below I am validating page requests and then want to cause a simple send of the known page to the browser. What class and method should I use? Thanks Steve ---------------------------------------------------------------------------- import os, sys from twisted.web import http, static, server, resource from twisted.internet.protocol import Factory from twisted.internet import reactor class RequestHandler(http.Request): pageHandlers = { 'index.html': 'def', 'main.html': 'def', #'/posthandler': handlePost, } def process(self): filename = os.path.basename(self.path) print "[process] request = " + self.path if self.pageHandlers.has_key(filename): handler = self.pageHandlers[filename] if handler == 'def': ### What do I call here to simply get the page sent to the browser??? #### else: handler(self, filename) else: self.setHeader('Content-Type', 'text/html') self.setResponseCode(http.NOT_FOUND) self.write("<h1>Not Found</h1>Sorry, no such page.") self.finish() class ServerProtocol(http.HTTPChannel): requestFactory = RequestHandler os.chdir('D:\\html') factory = http.HTTPFactory() factory.protocol = ServerProtocol reactor.listenTCP(80, factory) reactor.run() _________________________________________________________________ Find the coolest online games @ http://xtramsn.co.nz/gaming
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 You should rarely have to subclass http.Request, in this case you really want to construct a root resource that validates the request in getChild[1]. After the request is validated you can return a Resource representing the page you want to serve. If these are static files static.File can be used to wrap them. Otherwise you'll probably have to construct a custom resource that solves your problem. See the twisted.web howto for more details.[2] [1] http://twistedmatrix.com/documents/current/api/ twisted.web.resource.Resource.html#getChild [2] http://twistedmatrix.com/projects/web/documentation/howto/using- twistedweb.html - -David http://dreid.org "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.5 (Darwin) iD8DBQFF7GxhrsrO6aeULcgRAl37AJ468ccWegVbR9I4YoVptO/1ZZ3e7wCfU+1l XLFnh9P1juPRCdL5UN70Ry8= =gJ2q -----END PGP SIGNATURE-----
participants (2)
-
David Reid
-
Steve doh