[Twisted-Python] session
Hi, I did an other litle example to see how can work session. A shop where you put some articles in your bag... I do request.getSession() and set attribute directly to the session... Is it the good way ? import twisted.web.resource import cgi class Shop(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ articles=("chocolate","banana","apple") def isLeaf(self): return true def render(self, request): session = request.getSession() try: bag = session.bag except AttributeError: bag = session.bag={} try: article = request.args["put"][0] bag[article] = session.bag.get(article,0)+1 except KeyError: pass # no article to put out = [] out.append("""<html><body>What do you want to put in your bag ?<br> <a href='?put=chocolate'>chocolate</a><br> <a href='?put=banana'>banana</a><br> <a href='?put=apple'>apple</a><br> """) if len(bag) == 0: out.append("Your bag is empty") else: out.append("You have in your bag :<br>") for k,v in bag.items(): out.append("%d %s<br>"%(v,k)) return "".join(out) from twisted.internet import reactor from twisted.web import static, server site = server.Site(Shop()) reactor.listenTCP(8080,site) reactor.run()
This is very interesting. I found out much of what was unclear when I posted my earlier posting to this list by looking at this code. But I have some more questions : - how do I send HTTP-headers to the client? - if I want to create some webservices, both XML-RPC and SOAP-based without starting a different server using something like the code below, how do I do that? I want to run one server, providing both webservices and content aimed at browsers. - is this the preferred way of doing things? Can I base my work on this example? Drawbacks? I need the fastest, most scalable method available, which can handle lots of concurrent users, several downloads and processes at the same time, both using simple web-pages, webservices like the ones mentioned earlier and ftp-access for administrators to maintain files served by the server, both static files and source-code. The server should be able to reload, using updated source without a complete restart. That would be the ultimate platform. I'm still a bit unclear on wheter Twisted is a async. *blocking* framework, making concurrent downloads harder, if at all possible, or if it is similar to the threaded solution available in the standard python distr. I've based my solutions on a threaded version of BaseHTTPServer so far, and been told this is not very scalable. But I need a non-blocking solution, where one process doesn't lock the entire server until that process is over before other clients are handled. Any clues, hints, code or flames appreciated. Thanks so far. Twisted looks great !! :-) Best regards, Thomas ----- Original Message ----- From: "William Dode" <wilk-ml@flibuste.net> To: <twisted-python@twistedmatrix.com> Sent: Saturday, February 15, 2003 7:11 PM Subject: [Twisted-Python] session
Hi,
I did an other litle example to see how can work session. A shop where you put some articles in your bag...
I do request.getSession() and set attribute directly to the session... Is it the good way ?
import twisted.web.resource import cgi
class Shop(twisted.web.resource.Resource): """ ReportRequest with forms and cookies """ articles=("chocolate","banana","apple") def isLeaf(self): return true def render(self, request): session = request.getSession() try: bag = session.bag except AttributeError: bag = session.bag={}
try: article = request.args["put"][0] bag[article] = session.bag.get(article,0)+1 except KeyError: pass # no article to put
out = [] out.append("""<html><body>What do you want to put in your bag ?<br> <a href='?put=chocolate'>chocolate</a><br> <a href='?put=banana'>banana</a><br> <a href='?put=apple'>apple</a><br> """) if len(bag) == 0: out.append("Your bag is empty") else: out.append("You have in your bag :<br>") for k,v in bag.items(): out.append("%d %s<br>"%(v,k)) return "".join(out)
from twisted.internet import reactor from twisted.web import static, server
site = server.Site(Shop()) reactor.listenTCP(8080,site) reactor.run()
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
On Sun, Feb 16, 2003 at 01:08:04AM +0100, Thomas Weholt wrote:
by the server, both static files and source-code. The server should be able to reload, using updated source without a complete restart. That would be the ultimate platform. I'm still a bit unclear on wheter Twisted is a async. *blocking* framework, making concurrent downloads harder, if at all possible, or if it is similar to the threaded solution available in the standard python distr. I've based my solutions on a threaded version of
For reloading, look at twisted.python.rebuild. And yes, Twisted is an async *non-blocking* framework, so it can merrily handle multiple concurrent downloads. And uploads. For several different protocols at the same time :) -Andrew.
"Thomas Weholt" <2002@weholt.org> writes:
This is very interesting. I found out much of what was unclear when I posted my earlier posting to this list by looking at this code. But I have some more questions :
if twisted guru could confirm that it's the right way ? i could make a little documentation cgi<->twisted
- how do I send HTTP-headers to the client?
request.setHeader(self, k, v) Set an outgoing HTTP header. (inherited from Request) for example setHeader("content-type","text/csv")
- if I want to create some webservices, both XML-RPC and SOAP-based without starting a different server using something like the code below, how do I do that? I want to run one server, providing both webservices and content aimed at browsers.
- is this the preferred way of doing things? Can I base my work on this example? Drawbacks? I need the fastest, most scalable method available, which can handle lots of concurrent users, several downloads and processes at the same time, both using simple web-pages, webservices like the ones mentioned earlier and ftp-access for administrators to maintain files served by the server, both static files and source-code. The server should be able to reload, using updated source without a complete restart. That would be the ultimate platform. I'm still a bit unclear on wheter Twisted is a async. *blocking* framework, making concurrent downloads harder, if at all possible, or if it is similar to the threaded solution available in the standard python distr. I've based my solutions on a threaded version of BaseHTTPServer so far, and been told this is not very scalable. But I need a non-blocking solution, where one process doesn't lock the entire server until that process is over before other clients are handled.
The IO are not blocked, you can upload/download big files in the same times without worry about this, even if the client are slow. Thread are blocked, you have to "defer" when you will do long task. In the archive of the mailing-list, there is a good example of using defered with sql query. -- William Dode - http://flibuste.net
On Sun, Feb 16, 2003 at 01:08:04AM +0100, Thomas Weholt wrote:
- how do I send HTTP-headers to the client?
That's documented in Request's API ;-)
- if I want to create some webservices, both XML-RPC and SOAP-based without starting a different server using something like the code below, how do I do that? I want to run one server, providing both webservices and content aimed at browsers.
- is this the preferred way of doing things? Can I base my work on this
No -- you want to use .rpys (.rpys can be used for XML-RPC resources too, of course). William made some nice changes to that example, but changing it to instantiate its own server.Site was a bad one.
I'm still a bit unclear on wheter Twisted is a async. *blocking* framework, making concurrent downloads harder, if at all
No, Twisted is non-blocking. i.e., NEVER write code that blocks in Twisted applications, unless you know what you're doing :-) -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
That's documented in Request's API ;-)
Yes, after some digging around I found out that the API-docs explained alot. I'll try there first in the future.
- is this the preferred way of doing things? Can I base my work on this
No -- you want to use .rpys (.rpys can be used for XML-RPC resources too, of course). William made some nice changes to that example, but changing it to instantiate its own server.Site was a bad one.
Ok. I liked that way, but I'm porting a project from a solution based on BaseHTTPServer and a very customized RequestHandler to Twisted and like to do it the Twisted-way as much as possible. But I need to have some middle-man between the server and the .rpy that will produce the content. So far I've decided what method to use to produce content by path: /test/index.html would be something like : server.webapps['test'].index(request, response, server) in my earlier code. ( Actually the method called was found using getattr and some ugly code, so if there's a better way I'm read to learn. ) I cannot get the grip on how to use childs and leafs in Twisted. I think my code should be written to use that somehow. The reason I need this is because I need to have a perstistent object, containing compiled templates ( using simpleTAL/ES ), users-information and a dict of what I've called webapps, similar to the rpys I guess, in memory. I think that can work with some of the sample code I've seen. But I want to pass more parameters to the rpy-file that will produce the content, not just call it's render method. In short; how can I turn Williams example into a rpy and still have the ability to decide what render-method to call based on the requested path and pass more parameters to the rendering method? Is that possible? I don't want to write an rpy for each file that will produce on page. In my previous solution I've seperated businesslogic, data and content-generation in such a way that the content-producer method took only a few lines each. It made it possible to have them in one class, instead of a bunch of small files. It could look something like this ; class MyPage: def index(self, request, response, server): # produces content for index.html def about(self, request, response, server): # produces content for about.html ... etc ... It made it easier to maintain, easier to reuse code between content-generator methods and overall easier to understand. I think. Since design was in templates all I did was build a dict of values, find the right template and call the proper method in simpleTAL/ES to produce the content with the dict and name of template as parameters. It was very easy and done in just a few lines of code.
Twisted applications, unless you know what you're doing :-)
So far I'm without a clue, but hopefully I'm getting there ;-) Thanks. Sorry if I'm writing a bunch of crap here, but I'm very excited about getting the hang of Twisted. Best regards, Thomas
On Sunday, February 16, 2003, at 09:06 AM, Thomas Weholt wrote:
No -- you want to use .rpys (.rpys can be used for XML-RPC resources too, of course). William made some nice changes to that example, but changing it to instantiate its own server.Site was a bad one.
I cannot get the grip on how to use childs and leafs in Twisted. I think my code should be written to use that somehow. The reason I need this is because I need to have a perstistent object, containing compiled templates ( using simple TAL/ES ), users-information and a dict of what I've called webapps, similar to the rpys I guess, in memory. I think that can work with some of the sample code I've seen. But I want to pass more parameters to the rpy-file that will produce the content, not just call it's render method.
You want to use twisted.web.woven.tapestry: however, it's not entirely ready. At last I knew of it, this module was going to be deprecated and functionality was going to be rolled back into Controller. At any rate, reading the code there should give you some ideas. Nothing horribly confusing is going to happen to it though. You will probably have to spend 30 minutes renaming some classes when we polish and finalize it. As to the theory: An RPY is a file that can generate a resource. It is NOT suggested, in an application, to have multiple related resources in different .rpy's. They are conceptually related objects, which you can aggregate by using request.putChild and overriding request.getChild. What you want to do is to create a top-level "site" resource, (not a server.Site) and then handle getChild there, returning other Resource instances that you can dynamically create, cache, or load from your own files. As far as "leafs" -- in Twisted Web, isLeaf is just a flag that says "stop calling getChild here, please". There's nothing really special about it. A simple example: http://asdf.com/foo/bar/baz => root.getChild("foo", ...).getChild("bar", ...).getChild("baz", ...).render(...) What happens at each step of that path is pretty much up to you ;).
Christopher Armstrong <radix@twistedmatrix.com> writes:
you want to use .rpys
A quick look through the documentation index doesn't show anything that is obviously about .rpys. How should the uninitiated be finding out that they want to use them, if they don't even see a mention? -Justin
On Sun, Feb 16, 2003 at 10:31:46AM -0500, justin@iago.org wrote:
Christopher Armstrong <radix@twistedmatrix.com> writes:
you want to use .rpys
A quick look through the documentation index doesn't show anything that is obviously about .rpys. How should the uninitiated be finding out that they want to use them, if they don't even see a mention?
Don't look so quickly. http://twistedmatrix.com/documents/howto/using-twistedweb There's an entire section about them there. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
On Sun, Feb 16, 2003 at 10:31:46AM -0500, justin@iago.org wrote:
Christopher Armstrong <radix@twistedmatrix.com> writes:
you want to use .rpys
A quick look through the documentation index doesn't show anything that is obviously about .rpys. How should the uninitiated be finding out that they want to use them, if they don't even see a mention?
Don't look so quickly.
http://twistedmatrix.com/documents/howto/using-twistedweb
There's an entire section about them there.
It's a bit unclear on where to put all the files created in the example, where to put files to make them available in the generated *.tap-file etc. Some lines about file organisation would be nice. More examples, more details, step-by-step, for dummies approach, -- that's what I'm after. Apparently I'm not the only one having trouble understanding the concepts of rpys, resources and what not. I'm slowly getting a grip on it, but there are still some things I must do that I don't understand. If I get the time I'll try to document my findings and make a more step-by-step guide for complete dummies on the matter. Still, it seems like Twisted has enourmos potential. :-) Thomas
On Sun, 16 Feb 2003, "Thomas Weholt" <2002@weholt.org> wrote:
It's a bit unclear on where to put all the files created in the example, where to put files to make them available in the generated *.tap-file etc. Some lines about file organisation would be nice. More examples, more details, step-by-step, for dummies approach, -- that's what I'm after. Apparently I'm not the only one having trouble understanding the concepts of rpys, resources and what not.
One of the tutorials for the upcoming PyCon is about Deployment, Configuration and Usage of the Twisted Web Server (by Travis B. Hartwell). Just in case any of you going there, this should be a pretty good introduction. -- Moshe Zadka -- http://moshez.org/ Buffy: I don't like you hanging out with someone that... short. Riley: Yeah, a lot of young people nowadays are experimenting with shortness.
Christopher Armstrong <radix@twistedmatrix.com> writes:
On Sun, Feb 16, 2003 at 10:31:46AM -0500, justin@iago.org wrote:
Christopher Armstrong <radix@twistedmatrix.com> writes:
you want to use .rpys
A quick look through the documentation index doesn't show anything that is obviously about .rpys. How should the uninitiated be finding out that they want to use them, if they don't even see a mention?
Don't look so quickly.
http://twistedmatrix.com/documents/howto/using-twistedweb
There's an entire section about them there.
Thanks. Now I know what they are. However, that doesn't really answer the point I was making. Starting from the roots of the documentation, I would not have found that, as I am not using or intending to use the Twisted Web server. If these are actually recommended for general use, then they should be presented in a non-domain-specific section of the documentation. It would also be good if they were presented in context showing their purpose, capbilities and role, perhaps in comparison to TAPs. -Justin
On Mon, Feb 24, 2003 at 05:45:42PM -0500, justin@iago.org wrote:
Christopher Armstrong <radix@twistedmatrix.com> writes:
On Sun, Feb 16, 2003 at 10:31:46AM -0500, justin@iago.org wrote:
A quick look through the documentation index doesn't show anything that is obviously about .rpys. How should the uninitiated be finding out that they want to use them, if they don't even see a mention?
Don't look so quickly.
http://twistedmatrix.com/documents/howto/using-twistedweb
There's an entire section about them there.
Thanks. Now I know what they are.
However, that doesn't really answer the point I was making.
Starting from the roots of the documentation, I would not have found that, as I am not using or intending to use the Twisted Web server.
If these are actually recommended for general use, then they should be presented in a non-domain-specific section of the documentation.
It would also be good if they were presented in context showing their purpose, capbilities and role, perhaps in comparison to TAPs.
I'm not really sure what you mean... .rpys are specifically for twisted.web. If you're not planning on using or developing stuff for twisted.web, they're totally irrelevant for you. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | Release Manager, Twisted Project ---------+ http://twistedmatrix.com/users/radix.twistd/
participants (7)
-
Andrew Bennetts -
Christopher Armstrong -
Glyph Lefkowitz -
justin@iago.org -
Moshe Zadka -
Thomas Weholt -
William Dode