On Tuesday 24 Feb 2004 7:28 pm, Alex Levy wrote:
On Tue, 2004-02-24 at 13:35, Phil Hunt wrote:
I am trying to get my head round Twisted, particularly to use it as a simple web server (using http as a RPC mechanism, really). I was thinknig of using a "Rosetta Stone" approach, and am wondering if someone could translate this ordinary Python program into Twisted, please:
Attached are two examples of what you've described, both of which are a lot easier on the eyes than SimpleHTTPServer code.
rosetta1 just uses twisted.web's Resource object, which is relatively basic in its capabilities. If you want more scalable and flexible templating and web application design, you might want to start working with Nevow, which is what rosetta2 demonstrates.
Both of these scripts should be run using the following command:
$ twistd -ony rosettaX.tac.py
rosetta1 kind-of does what I want, but not quite, when I try to fetch the URL <http://localhost:1450/xxx?aaa=bbb&c=12345> in my web browser, it says request.path = /xxx, whereas I'm interested in the variable/value pairs as well, i.e. /xxx?aaa=bbb&c=12345 If I wrote a program like this that was invoked by the ``python'' program rather than ``twistd'', would that make a difference it what it can do? I ask because I would like to get such a program to talk to other data sources. For example, I'm writing a test program that sends an http GET request, then tests whether it receives a response (also an http GET request, but from a 3rd process), within a certain time span, such as 2 seconds. IOW, I have three programs: TH -----------> P2 ^ | | | `------------- P3 TH is a test harness, that is both a web client and a web server. TH sends http GET requests to P2, then receives them (some time later) from P3. P2 is a web server; it receives GET requests but does not send them. P3 is a web client; it sends GET requests but does not receive them. (Am I barking up the wriong tree here? I'm using http because it is a simple protocol, and because one side of the protocol can be emulated by Mozilla, making it easier from me to test my software when not all the components have been written. But maybe there's a simpler way?) Also, how easy is it to write a full python [program with a reactor as opposed to a .tac.py? Are there advantages in doing so? (I'm assuming that with .tac.py programs, the stuff to do with the reactor is handled by twistd). I want to invoke my programs with options at the command line (the port number will be one of these options) -- can I do that with twistd? -- Phil Hunt, phil.hunt@tech.mrc.ac.uk