Ticket #4576: web.xhtml.patch
| File web.xhtml.patch, 20.0 KB (added by jdb, 3 years ago) |
|---|
-
doc/web/howto/resource-templates.xhtml
diff --git a/doc/web/howto/resource-templates.xhtml b/doc/web/howto/resource-templates.xhtml index be7226a..b34d183 100644
a b 12 12 13 13 <h2>Overview</h2> 14 14 15 <p>While high-level templating systems can be used with Twisted (for example, 16 <a href="http://divmod.org/trac/wiki/DivmodNevow">Divmod Nevow</a>, sometimes 17 one needs a less file-heavy system which lets one directly write HTML. While 18 ResourceScripts are available, they have a high overhead of coding, needing 19 some boring string arithmetic. ResourceTemplates fill the space between Nevow 20 and ResourceScript using Quixote's PTL (Python Templating Language).</p> 21 22 <p>ResourceTemplates need Quixote installed. In 23 <a href="http://www.debian.org">Debian</a>, that means using Python 2.2 24 and installing the <code>quixote</code> package 25 (<code>apt-get install quixote</code>). Other operating systems require 26 other ways to install quixote, or it can be done manually.</p> 15 <p>While high-level templating systems can be used with Twisted (for 16 example, <a href="http://divmod.org/trac/wiki/DivmodNevow">Divmod 17 Nevow</a>, sometimes one needs a less file-heavy system which lets one 18 directly write HTML. While ResourceScripts are available, they have a 19 high overhead of coding, needing some boring string 20 arithmetic. ResourceTemplates fill the space between Nevow and 21 ResourceScript using Quixote's PTL (Python Templating Language).</p> 22 23 <p>ResourceTemplates need Quixote 24 installed. In <a href="http://www.debian.org">Debian</a>, that means 25 using Python 2.2 and installing the <code>quixote</code> package 26 (<code>apt-get install quixote</code>). Other operating systems 27 require other ways to install quixote, or it can be done manually.</p> 27 28 28 29 <h2>Configuring Twisted.Web</h2> 29 30 … … 44 45 45 46 <p>ResourceTemplates are coded in an extension of Python called the 46 47 <q>Python Templating Language</q>. Complete documentation of the PTL 47 is available at <a href="http://www.mems-exchange.org/software/quixote/doc/PTL.html">the quixote web site</a>. The web server 48 will expect the PTL source file to define a variable named 49 <code>resource</code>. 50 This should be a <code class="API">twisted.web.server.Resource</code>, 48 is available 49 at <a href="http://www.mems-exchange.org/software/quixote/doc/PTL.html">the 50 quixote web site</a>. The web server will expect the PTL source file 51 to define a variable named <code>resource</code>. This should be 52 a <code class="API">twisted.web.server.Resource</code>, 51 53 whose <code>.render</code> method be called. Usually, you would want 52 54 to define <code>render</code> using the keyword <code>template</code> 53 55 rather than <code>def</code>.</p> -
doc/web/howto/using-twistedweb.xhtml
diff --git a/doc/web/howto/using-twistedweb.xhtml b/doc/web/howto/using-twistedweb.xhtml index 5c4c388..b681a2c 100644
a b 21 21 22 22 <ul> 23 23 24 <li><a href="#sites">Site Objects</a> are responsible for creating 25 <code>HTTPChannel</code> instances to parse the HTTP request, and begin the object lookup process. They contain the root Resource, the resource which represents the URL <code>/</code> on the site.</li> 24 <li><a href="#sites">Site Objects</a> are responsible for 25 creating <code>HTTPChannel</code> instances to parse the HTTP request, 26 and begin the object lookup process. They contain the root Resource, 27 the resource which represents the URL <code>/</code> on the site.</li> 26 28 27 29 <li><a href="#resources">Resource</a> objects represent a single URL segment. The <code class="API" base="twisted.web.resource">IResource</code> interface describes the methods a Resource object must implement in order to participate in the object publishing process.</li> 28 30 … … 49 51 50 52 <p>When using <code>twistd -n web --path /foo/bar/baz</code>, a Site object is created with a root Resource that serves files out of the given path.</p> 51 53 52 <p>You can also create a <code>Site</code> instance by hand, passing it a 53 <code>Resource</code> object which will serve as the root of the site:</p> 54 <p>You can also create a <code>Site</code> instance by hand, passing 55 it a <code>Resource</code> object which will serve as the root of the 56 site:</p> 54 57 55 58 <pre class="python"> 56 59 from twisted.web import server, resource … … 173 176 174 177 The <code class="API" base="twisted.web.resource">Resource</code> 175 178 class, which is usually what one's Resource classes subclass, has a 176 convenient default implementation of <code177 class="python">render</code>. It will call a method named <code 178 class="python">self.render_METHOD</code> where <q>METHOD</q> is 179 wh atever HTTP method was used to request this resource. Examples:180 re quest_GET, request_POST, request_HEAD, and so on. It is recommended181 that you have your resource classes subclass <code class="API" 182 base="twisted.web.resource">Resource</code> and implement <code 183 class="python">render_METHOD</code> methods as opposed to <code 184 class="python">render</code> itself. Note that for certain resources, 185 <code class="python">request_POST = request_GET</code> may be 186 desirable in case one wants to process arguments passed to the 187 resource regardless of whether they used GET179 convenient default implementation 180 of <code class="python">render</code>. It will call a method 181 named <code class="python">self.render_METHOD</code> 182 where <q>METHOD</q> is whatever HTTP method was used to request this 183 resource. Examples: request_GET, request_POST, request_HEAD, and so 184 on. It is recommended that you have your resource classes 185 subclass <code class="API" base="twisted.web.resource">Resource</code> 186 and implement <code class="python">render_METHOD</code> methods as 187 opposed to <code class="python">render</code> itself. Note that for 188 certain resources, <code class="python">request_POST = 189 request_GET</code> may be desirable in case one wants to process 190 arguments passed to the resource regardless of whether they used GET 188 191 (<code>?foo=bar&baz=quux</code>, and so forth) or POST. 189 192 190 193 </p> … … 201 204 <h2>Advanced Configuration</h2> 202 205 203 206 <p>Non-trivial configurations of Twisted Web are achieved with Python 204 configuration files. This is a Python snippet which builds up a variable 205 called application. Usually, a <code class="API">twisted.application.internet.TCPServer</code> instance will 206 be used to make the application listen on a TCP port (80, in case direct 207 web serving is desired), with the listener being a 208 <code class="API">twisted.web.server.Site</code>. The resulting file can then 209 be run with <code class="shell">twistd -y</code>. Alternatively a reactor 210 object can be used directly to make a runnable script.</p> 207 configuration files. This is a Python snippet which builds up a 208 variable called application. Usually, 209 a <code class="API">twisted.application.internet.TCPServer</code> 210 instance will be used to make the application listen on a TCP port 211 (80, in case direct web serving is desired), with the listener being 212 a <code class="API">twisted.web.server.Site</code>. The resulting file 213 can then be run with <code class="shell">twistd 214 -y</code>. Alternatively a reactor object can be used directly to make 215 a runnable script.</p> 211 216 212 217 <p>The <code>Site</code> will wrap a <code>Resource</code> object -- the 213 218 root.</p> … … 259 264 260 265 <h3>Modifying File Resources</h3> 261 266 262 <p><code>File</code> resources, be they root object or children thereof, 263 have two important attributes that often need to be modified: 264 <code>indexNames</code> and <code>processors</code>. <code>indexNames</code> 265 determines which files are treated as <q>index files</q> -- served 266 up when a directory is rendered. <code>processors</code> determine how 267 certain file extensions are treated.</p> 267 <p><code>File</code> resources, be they root object or children 268 thereof, have two important attributes that often need to be 269 modified: <code>indexNames</code> 270 and <code>processors</code>. <code>indexNames</code> determines which 271 files are treated as <q>index files</q> -- served up when a directory 272 is rendered. <code>processors</code> determine how certain file 273 extensions are treated.</p> 268 274 269 275 <p>Here is an example for both, creating a site where all <code>.rpy</code> 270 276 extensions are Resource Scripts, and which renders directories by … … 308 314 309 315 <h3>Virtual Hosts</h3> 310 316 311 <p>Virtual hosting is done via a special resource, that should be 312 used as the root resource -- <code>NameVirtualHost</code>. 313 <code>NameVirtualHost</code> has an attribute named <code>default</code>, 314 which holds the default website. If a different root for some other 315 name is desired, the <code>addHost</code> method should be called.</p> 317 <p>Virtual hosting is done via a special resource, that should be used 318 as the root resource 319 -- <code>NameVirtualHost</code>. <code>NameVirtualHost</code> has an 320 attribute named <code>default</code>, which holds the default 321 website. If a different root for some other name is desired, 322 the <code>addHost</code> method should be called.</p> 316 323 317 324 <pre class="python"> 318 325 from twisted.application import internet, service … … 529 536 530 537 <h3>Serving PHP/Perl/CGI</h3> 531 538 532 <p>Everything related to CGI is located in the 533 <code>twisted.web.twcgi</code>, and it's here you'll find the classes that you 534 need to subclass in order to support the language of your (or somebody elses) 535 taste. You'll also need to create your own kind of resource if you are using a 536 non-unix operating system (such as Windows), or if the default resources has 537 wrong pathnames to the parsers.</p> 539 <p>Everything related to CGI is located in 540 the <code>twisted.web.twcgi</code>, and it's here you'll find the 541 classes that you need to subclass in order to support the language of 542 your (or somebody elses) taste. You'll also need to create your own 543 kind of resource if you are using a non-unix operating system (such as 544 Windows), or if the default resources has wrong pathnames to the 545 parsers.</p> 538 546 539 547 <p>The following snippet is a .rpy that serves perl-files. Look at <code>twisted.web.twcgi</code> 540 548 for more examples regarding twisted.web and CGI.</p> … … 643 651 resource = vhost.VHostMonsterResource() 644 652 </pre> 645 653 646 <p>Make sure the web server is configured with the correct processors for the647 <code>rpy</code> extensions (the web server 648 <code>twistd web--path</code> generates by default is so configured).</p>654 <p>Make sure the web server is configured with the correct processors 655 for the <code>rpy</code> extensions (the web server <code>twistd web 656 --path</code> generates by default is so configured).</p> 649 657 650 658 <p>From the Apache side, instead of using the following ProxyPass directive:</p> 651 659 … … 684 692 685 693 <h2>Rewriting URLs</h2> 686 694 687 <p>Sometimes it is convenient to modify the content of the688 <code class="API" base="twisted.web.server">Request</code> object695 <p>Sometimes it is convenient to modify the content of 696 the <code class="API" base="twisted.web.server">Request</code> object 689 697 before passing it on. Because this is most often used to rewrite 690 either the URL, the similarity to Apache's <code>mod_rewrite</code> has691 inspired the <code class="API">twisted.web.rewrite</code> module. Using 692 this module is done via wrapping a resource with a 693 <code class="API">twisted.web.rewrite.RewriterResource</code> which694 then has rewrite rules. Rewrite rules are functions which accept a request695 object, and possible modify it. After all rewrite rules run, the child 696 resolution chain continues as if the wrapped resource, rather than 697 the <code class="API" base="twisted.web.rewrite">RewriterResource</code>, 698 was the child.</p>698 either the URL, the similarity to Apache's <code>mod_rewrite</code> 699 has inspired the <code class="API">twisted.web.rewrite</code> 700 module. Using this module is done via wrapping a resource with 701 a <code class="API">twisted.web.rewrite.RewriterResource</code> which 702 then has rewrite rules. Rewrite rules are functions which accept a 703 request object, and possible modify it. After all rewrite rules run, 704 the child resolution chain continues as if the wrapped resource, 705 rather than the <code class="API" 706 base="twisted.web.rewrite">RewriterResource</code>, was the child.</p> 699 707 700 708 <p>Here is an example, using the only rule currently supplied by Twisted 701 709 itself:</p> … … 704 712 default_root = rewrite.RewriterResource(default, rewrite.tildeToUsers) 705 713 </pre> 706 714 707 <p>This causes the URL <code>/~foo/bar.html</code> to be treated like708 <code>/users/foo/bar.html</code>. If done after setting default's 709 <code>users</code> child to a 710 <code class="API" base="twisted.web">distrib.UserDirectory</code>, 711 it gives a configuration similar to the classical configuration of 712 web server,common since the first NCSA servers.</p>715 <p>This causes the URL <code>/~foo/bar.html</code> to be treated 716 like <code>/users/foo/bar.html</code>. If done after setting 717 default's <code>users</code> child to a <code class="API" 718 base="twisted.web">distrib.UserDirectory</code>, it gives a 719 configuration similar to the classical configuration of web server, 720 common since the first NCSA servers.</p> 713 721 714 722 <h2>Knowing When We're Not Wanted</h2> 715 723 … … 741 749 742 750 <h2>As-Is Serving</h2> 743 751 744 <p>Sometimes, you want to be able to send headers and status directly. While745 you can do this with a 746 <code base="twisted.web.script" class="API">ResourceScript</code>, an easier 747 way is touse <code base="twisted.web.static" class="API">ASISProcessor</code>.748 Use it by, for example, adding it as a processor for the <code>.asis</code>749 extension. Here is a sample file:</p>752 <p>Sometimes, you want to be able to send headers and status 753 directly. While you can do this with a <code base="twisted.web.script" 754 class="API">ResourceScript</code>, an easier way is to 755 use <code base="twisted.web.static" class="API">ASISProcessor</code>. 756 Use it by, for example, adding it as a processor for 757 the <code>.asis</code> extension. Here is a sample file:</p> 750 758 751 759 <pre> 752 760 HTTP/1.0 200 OK -
doc/web/howto/web-development.xhtml
diff --git a/doc/web/howto/web-development.xhtml b/doc/web/howto/web-development.xhtml index 0a987d1..562cccf 100644
a b 82 82 deployment with development, and makes sure your users will be <em>tied</em> to 83 83 the file-system.</p> 84 84 85 <p>We have <code>.rpy</code>s because they are useful and necessary. But using 86 them incorrectly leads to horribly unmaintainable applications. The best way to 87 ensure you are using them correctly is to not use them at all, until you are on 88 your <em>final</em> deployment stages. You should then find your 89 <code>.rpy</code> files will be less than 10 lines, because you will not 90 <em>have</em> more than 10 lines to write.</p> 85 <p>We have <code>.rpy</code>s because they are useful and necessary. 86 But using them incorrectly leads to horribly unmaintainable 87 applications. The best way to ensure you are using them correctly is 88 to not use them at all, until you are on your <em>final</em> 89 deployment stages. You should then find your <code>.rpy</code> files 90 will be less than 10 lines, because you will not <em>have</em> more 91 than 10 lines to write.</p> 91 92 92 93 </body> 93 94 -
doc/web/howto/xmlrpc.xhtml
diff --git a/doc/web/howto/xmlrpc.xhtml b/doc/web/howto/xmlrpc.xhtml index 38001e4..b0bb5cc 100644
a b 99 99 <h3>Using XML-RPC sub-handlers</h3> 100 100 101 101 <p>XML-RPC resource can be nested so that one handler calls another if 102 a method with a given prefix is called. For example, to add support for 103 an XML-RPC method <code>date.time()</code> to the 104 <code class="python">Example</code> class, you could do the following:</p> 102 a method with a given prefix is called. For example, to add support 103 for an XML-RPC method <code>date.time()</code> to 104 the <code class="python">Example</code> class, you could do the 105 following:</p> 105 106 106 107 <pre class="python"> 107 108 import time … … 142 143 143 144 <h3>Adding XML-RPC Introspection support</h3> 144 145 145 <p>XML-RPC has an informal <a href="http://ldp.kernelnotes.de/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-interfaces.html">Introspection API</a> that specifies three 146 methods in a <code>system</code> sub-handler which allow a client to query 147 a server about the server's API. Adding Introspection support to the 148 <code class="python">Example</code> class is easy using the 149 <code base="twisted.web.xmlrpc" class="API">XMLRPCIntrospection</code> 150 class:</p> 146 <p>XML-RPC has an 147 informal <a href="http://ldp.kernelnotes.de/HOWTO/XML-RPC-HOWTO/xmlrpc-howto-interfaces.html">Introspection 148 API</a> that specifies three methods in a <code>system</code> 149 sub-handler which allow a client to query a server about the server's 150 API. Adding Introspection support to 151 the <code class="python">Example</code> class is easy using 152 the <code base="twisted.web.xmlrpc" 153 class="API">XMLRPCIntrospection</code> class:</p> 151 154 152 155 <pre class="python"> 153 156 from twisted.web import xmlrpc, server … … 181 184 reactor.run() 182 185 </pre> 183 186 184 <p>Note the method attributes <code class="python">help</code> and185 <code class="python">signature</code> which are used by the Introspection 186 API methods <code>system.methodHelp</code> and 187 <code>system.methodSignature</code> respectively. If no 188 <code class="python">help</code> attribute is specified, 189 themethod's documentation string is used instead.</p>187 <p>Note the method attributes <code class="python">help</code> 188 and <code class="python">signature</code> which are used by the 189 Introspection API methods <code>system.methodHelp</code> 190 and <code>system.methodSignature</code> respectively. If 191 no <code class="python">help</code> attribute is specified, the 192 method's documentation string is used instead.</p> 190 193 191 194 <h2>SOAP Support</h2> 192 195 … … 210 213 <code>callRemote</code>.</li> 211 214 </ol> 212 215 213 <p>The interface Twisted presents to XML-RPC client is that of a proxy object: 214 <code class="API">twisted.web.xmlrpc.Proxy</code>. The constructor for the 215 object receives a URL: it must be an HTTP or HTTPS URL. When an XML-RPC service 216 is described, the URL to that service will be given there.</p> 216 <p>The interface Twisted presents to XML-RPC client is that of a proxy 217 object: <code class="API">twisted.web.xmlrpc.Proxy</code>. The 218 constructor for the object receives a URL: it must be an HTTP or HTTPS 219 URL. When an XML-RPC service is described, the URL to that service 220 will be given there.</p> 217 221 218 222 <p>Having a proxy object, one can just call the <code>callRemote</code> method, 219 223 which accepts a method name and a variable argument list (but no named … … 237 241 print 'error', error 238 242 reactor.stop() 239 243 240 proxy = Proxy('http://advogato.o rg/XMLRPC')244 proxy = Proxy('http://advogato.oubunturg/XMLRPC') 241 245 proxy.callRemote('test.sumprod', 3, 5).addCallbacks(printValue, printError) 242 246 reactor.run() 243 247 </pre> … … 257 261 base="twisted.web.resource.IResource">putChild</code> method of Resources.</p> 258 262 259 263 <p>The following example uses an empty <code class="API" 260 base="twisted.web">resource.Resource</code> as the root resource for a <code261 class="API" base="twisted.web.server">Site</code>, and then adds 262 <code>/RPC2</code> and <code>/SOAP</code> paths to it.</p>264 base="twisted.web">resource.Resource</code> as the root resource for 265 a <code class="API" base="twisted.web.server">Site</code>, and then 266 adds <code>/RPC2</code> and <code>/SOAP</code> paths to it.</p> 263 267 264 268 <a href="listings/xmlAndSoapQuote.py" class="py-listing">xmlAndSoapQuote.py</a> 265 269 266 < p>Refer to <a href="using-twistedweb.xhtml#development">Twisted Web270 <dh p>Refer to <a href="using-twistedweb.xhtml#development">Twisted Web 267 271 Development</a> for more details about Resources.</p> 268 272 269 273 </body>
