1 2:LastChangedDate: $LastChangedDate$ 3:LastChangedRevision: $LastChangedRevision$ 4:LastChangedBy: $LastChangedBy$ 5 6Configuring and Using the Twisted Web Server 7============================================ 8 9 10 11 12 13 14Twisted Web Development 15----------------------- 16.. _web-howto-using-twistedweb-development: 17 18 19 20 21 22 23 24 25Twisted Web serves Python objects that implement the interface 26IResource. 27 28 29 30 31 32 33.. image:: ../img/web-process.png 34 35 36 37 38 39Main Concepts 40~~~~~~~~~~~~~ 41 42 43 44 45 46- :ref:`Site Objects <web-howto-using-twistedweb-sites>` are responsible for 47 creating ``HTTPChannel`` instances to parse the HTTP request, 48 and begin the object lookup process. They contain the root Resource, 49 the resource which represents the URL ``/`` on the site. 50- :ref:`Resource <web-howto-using-twistedweb-resources>` objects represent a single URL segment. The :py:class:`IResource <twisted.web.resource.IResource>` interface describes the methods a Resource object must implement in order to participate in the object publishing process. 51- :ref:`Resource trees <web-howto-using-twistedweb-trees>` are arrangements of Resource objects into a Resource tree. Starting at the root Resource object, the tree of Resource objects defines the URLs which will be valid. 52- :ref:`.rpy scripts <web-howto-using-twistedweb-rpys>` are python scripts which the twisted.web static file server will execute, much like a CGI. However, unlike CGI they must create a Resource object which will be rendered when the URL is visited. 53- :ref:`Resource rendering <web-howto-using-twistedweb-rendering>` occurs when Twisted Web locates a leaf Resource object. A Resource can either return an html string or write to the request object. 54- :ref:`Session <web-howto-using-twistedweb-sessions>` objects allow you to store information across multiple requests. Each individual browser using the system has a unique Session instance. 55 56 57 58 59 60The Twisted Web server is started through the Twisted Daemonizer, as in: 61 62 63 64 65 66.. code-block:: console 67 68 69 % twistd web 70 71 72 73 74 75Site Objects 76~~~~~~~~~~~~ 77 78.. _web-howto-using-twistedweb-sites: 79 80 81 82 83 84 85 86 87Site objects serve as the glue between a port to listen for HTTP requests on, and a root Resource object. 88 89 90 91 92When using ``twistd -n web --path /foo/bar/baz`` , a Site object is created with a root Resource that serves files out of the given path. 93 94 95 96 97You can also create a ``Site`` instance by hand, passing 98it a ``Resource`` object which will serve as the root of the 99site: 100 101 102 103 104 105.. code-block:: python 106 107 108 from twisted.web import server, resource 109 from twisted.internet import reactor, endpoints 110 111 class Simple(resource.Resource): 112 isLeaf = True 113 def render_GET(self, request): 114 return b"<html>Hello, world!</html>" 115 116 site = server.Site(Simple()) 117 endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080) 118 endpoint.listen(site) 119 reactor.run() 120 121 122 123 124 125Resource objects 126~~~~~~~~~~~~~~~~ 127 128.. _web-howto-using-twistedweb-resources: 129 130 131 132 133 134 135 136 137``Resource`` objects represent a single URL segment of a site. During URL parsing, ``getChild`` is called on the current ``Resource`` to produce the next ``Resource`` object. 138 139 140 141 142When the leaf Resource is reached, either because there were no more URL segments or a Resource had isLeaf set to True, the leaf Resource is rendered by calling ``render(request)`` . See "Resource Rendering" below for more about this. 143 144 145 146 147During the Resource location process, the URL segments which have already been processed and those which have not yet been processed are available in ``request.prepath`` and ``request.postpath`` . 148 149 150 151 152A Resource can know where it is in the URL tree by looking at ``request.prepath`` , a list of URL segment strings. 153 154 155 156 157A Resource can know which path segments will be processed after it by looking at ``request.postpath`` . 158 159 160 161 162If the URL ends in a slash, for example ``http://example.com/foo/bar/`` , the final URL segment will be an empty string. Resources can thus know if they were requested with or without a final slash. 163 164 165 166 167Here is a simple Resource object: 168 169 170 171 172 173.. code-block:: python 174 175 176 from twisted.web.resource import Resource 177 178 class Hello(Resource): 179 isLeaf = True 180 def getChild(self, name, request): 181 if name == '': 182 return self 183 return Resource.getChild(self, name, request) 184 185 def render_GET(self, request): 186 output = "Hello, world! I am located at {}.".format(request.prepath) 187 return output.encode("utf8") 188 189 resource = Hello() 190 191 192 193 194 195Resource Trees 196~~~~~~~~~~~~~~ 197 198.. _web-howto-using-twistedweb-trees: 199 200 201 202 203 204 205 206 207Resources can be arranged in trees using ``putChild`` . ``putChild`` puts a Resource instance into another Resource instance, making it available at the given path segment name: 208 209 210 211 212 213.. code-block:: python 214 215 216 root = Hello() 217 root.putChild(b'fred', Hello()) 218 root.putChild(b'bob', Hello()) 219 220 221 222 223If this root resource is served as the root of a Site instance, the following URLs will all be valid: 224 225 226 227 228 229- ``http://example.com/`` 230- ``http://example.com/fred`` 231- ``http://example.com/bob`` 232- ``http://example.com/fred/`` 233- ``http://example.com/bob/`` 234 235 236 237 238 239 240.rpy scripts 241~~~~~~~~~~~~ 242 243.. _web-howto-using-twistedweb-rpys: 244 245 246 247 248 249 250 251 252Files with the extension ``.rpy`` are python scripts which, when placed in a directory served by Twisted Web, will be executed when visited through the web. 253 254 255 256 257An ``.rpy`` script must define a variable, ``resource`` , which is the Resource object that will render the request. 258 259 260 261 262``.rpy`` files are very convenient for rapid development and prototyping. Since they are executed on every web request, defining a Resource subclass in an ``.rpy`` will make viewing the results of changes to your class visible simply by refreshing the page: 263 264 265 266 267 268.. code-block:: python 269 270 271 from twisted.web.resource import Resource 272 273 class MyResource(Resource): 274 def render_GET(self, request): 275 return b"<html>Hello, world!</html>" 276 277 resource = MyResource() 278 279 280 281 282However, it is often a better idea to define Resource subclasses in Python modules. In order for changes in modules to be visible, you must either restart the Python process, or reload the module: 283 284 285 286 287 288.. code-block:: python 289 290 291 import myresource 292 293 ## Comment out this line when finished debugging 294 reload(myresource) 295 296 resource = myresource.MyResource() 297 298 299 300 301Creating a Twisted Web server which serves a directory is easy: 302 303 304 305 306 307.. code-block:: console 308 309 310 % twistd -n web --path /Users/dsp/Sites 311 312 313 314 315 316Resource rendering 317~~~~~~~~~~~~~~~~~~ 318 319.. _web-howto-using-twistedweb-rendering: 320 321 322 323 324 325 326 327 328Resource rendering occurs when Twisted Web locates a leaf Resource object to handle a web request. A Resource's ``render`` method may do various things to produce output which will be sent back to the browser: 329 330 331 332 333 334- Return a string 335- Call ``request.write(b"stuff")`` as many times as desired, then call ``request.finish()`` and return ``server.NOT_DONE_YET`` (This is deceptive, since you are in fact done with the request, but is the correct way to do this) 336- Request a ``Deferred`` , return ``server.NOT_DONE_YET`` , and call ``request.write("stuff")`` and ``request.finish()`` later, in a callback on the ``Deferred`` . 337 338 339 340 341 342 343 344The :py:class:`Resource <twisted.web.resource.Resource>` 345class, which is usually what one's Resource classes subclass, has a 346convenient default implementation 347of ``render`` . It will call a method 348named ``self.render_METHOD`` 349where "METHOD" is whatever HTTP method was used to request this 350resource. Examples: request_GET, request_POST, request_HEAD, and so 351on. It is recommended that you have your resource classes 352subclass :py:class:`Resource <twisted.web.resource.Resource>` 353and implement ``render_METHOD`` methods as 354opposed to ``render`` itself. Note that for 355certain resources, ``request_POST = request_GET`` may be desirable in case one wants to process 356arguments passed to the resource regardless of whether they used GET 357(``?foo=bar&baz=quux`` , and so forth) or POST. 358 359 360 361 362 363 364Request encoders 365~~~~~~~~~~~~~~~~ 366 367 368 369 370When using a :py:class:`Resource <twisted.web.resource.Resource>` , 371one can specify wrap it using a 372:py:class:`EncodingResourceWrapper <twisted.web.resource.EncodingResourceWrapper>` 373and passing a list of encoder factories. The encoder factories are 374called when a request is processed and potentially return an encoder. 375By default twisted provides 376:py:class:`GzipEncoderFactory <twisted.web.server.GzipEncoderFactory>` which 377manages standard gzip compression. You can use it this way: 378 379 380 381 382 383.. code-block:: python 384 385 386 from twisted.web.server import Site, GzipEncoderFactory 387 from twisted.web.resource import Resource, EncodingResourceWrapper 388 from twisted.internet import reactor, endpoints 389 390 class Simple(Resource): 391 isLeaf = True 392 def render_GET(self, request): 393 return b"<html>Hello, world!</html>" 394 395 resource = Simple() 396 wrapped = EncodingResourceWrapper(resource, [GzipEncoderFactory()]) 397 site = Site(wrapped) 398 endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080) 399 endpoint.listen(site) 400 reactor.run() 401 402 403 404 405 406Using compression on SSL served resources where the user can influence the 407content can lead to information leak, so be careful which resources use 408request encoders. 409 410 411 412 413 414Note that only encoder can be used per request: the first encoder factory 415returning an object will be used, so the order in which they are specified 416matters. 417 418 419 420 421 422Session 423~~~~~~~ 424 425.. _web-howto-using-twistedweb-sessions: 426 427 428 429 430 431 432 433 434HTTP is a stateless protocol; every request-response is treated as an individual unit, distinguishable from any other request only by the URL requested. With the advent of Cookies in the mid nineties, dynamic web servers gained the ability to distinguish between requests coming from different *browser sessions* by sending a Cookie to a browser. The browser then sends this cookie whenever it makes a request to a web server, allowing the server to track which requests come from which browser session. 435 436 437 438 439Twisted Web provides an abstraction of this browser-tracking behavior called the *Session object* . Calling ``request.getSession()`` checks to see if a session cookie has been set; if not, it creates a unique session id, creates a Session object, stores it in the Site, and returns it. If a session object already exists, the same session object is returned. In this way, you can store data specific to the session in the session object. 440 441 442 443 444 445.. image:: ../img/web-session.png 446 447 448 449 450 451Proxies and reverse proxies 452~~~~~~~~~~~~~~~~~~~~~~~~~~~ 453 454.. _web-howto-using-twistedweb-proxies: 455 456 457 458 459 460 461 462 463A proxy is a general term for a server that functions as an intermediary 464between clients and other servers. 465 466 467 468 469Twisted supports two main proxy variants: a :py:class:`Proxy <twisted.web.proxy.Proxy>` and a :py:class:`ReverseProxy <twisted.web.proxy.ReverseProxy>` . 470 471 472 473 474 475Proxy 476^^^^^ 477 478 479 480A proxy forwards requests made by a client to a destination server. Proxies 481typically sit on the internal network for a client or out on the internet, and 482have many uses, including caching, packet filtering, auditing, and circumventing 483local access restrictions to web content. 484 485 486 487 488Here is an example of a simple but complete web proxy: 489 490 491 492 493 494.. code-block:: python 495 496 497 from twisted.web import proxy, http 498 from twisted.internet import reactor, endpoints 499 500 class ProxyFactory(http.HTTPFactory): 501 def buildProtocol(self, addr): 502 return proxy.Proxy() 503 504 endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080) 505 endpoint.listen(ProxyFactory()) 506 reactor.run() 507 508 509 510 511With this proxy running, you can configure your web browser to use ``localhost:8080`` as a proxy. After doing so, when browsing the web 512all requests will go through this proxy. 513 514 515 516 517:py:class:`Proxy <twisted.web.proxy.Proxy>` inherits 518from :py:class:`http.HTTPChannel <twisted.web.http.HTTPChannel>` . Each client 519request to the proxy generates a :py:class:`ProxyRequest <twisted.web.proxy.ProxyRequest>` from the proxy to the destination 520server on behalf of the client. ``ProxyRequest`` uses 521a :py:class:`ProxyClientFactory <twisted.web.proxy.ProxyClientFactory>` to create 522an instance of the :py:class:`ProxyClient <twisted.web.proxy.ProxyClient>` 523protocol for the connection. ``ProxyClient`` inherits 524from :py:class:`http.HTTPClient <twisted.web.http.HTTPClient>` . Subclass ``ProxyRequest`` to 525customize the way requests are processed or logged. 526 527 528 529 530 531ReverseProxyResource 532^^^^^^^^^^^^^^^^^^^^ 533 534 535 536A reverse proxy retrieves resources from other servers on behalf of a 537client. Reverse proxies typically sit inside the server's internal network and 538are used for caching, application firewalls, and load balancing. 539 540 541 542 543Here is an example of a basic reverse proxy: 544 545 546 547 548 549.. code-block:: python 550 551 552 from twisted.internet import reactor, endpoints 553 from twisted.web import proxy, server 554 555 site = server.Site(proxy.ReverseProxyResource('www.yahoo.com', 80, '')) 556 endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080) 557 endpoint.listen(site) 558 reactor.run() 559 560 561 562 563With this reverse proxy running locally, you can 564visit ``http://localhost:8080`` in your web browser, and the reverse 565proxy will proxy your connection to ``www.yahoo.com``. 566 567 568 569 570In this example we use ``server.Site`` to serve 571a ``ReverseProxyResource`` directly. There is 572also a ``ReverseProxy`` family of classes 573in ``twisted.web.proxy`` mirroring those of the ``Proxy`` 574family: 575 576 577 578 579Like ``Proxy`` , :py:class:`ReverseProxy <twisted.web.proxy.ReverseProxy>` inherits 580from ``http.HTTPChannel`` . Each client request to the reverse proxy 581generates a :py:class:`ReverseProxyRequest <twisted.web.proxy.ReverseProxyRequest>` to the destination 582server. Like ``ProxyRequest`` , :py:class:`ReverseProxyRequest <twisted.web.proxy.ReverseProxyRequest>` uses a :py:class:`ProxyClientFactory <twisted.web.proxy.ProxyClientFactory>` to create an instance of 583the :py:class:`ProxyClient <twisted.web.proxy.ProxyClient>` protocol for 584the connection. 585 586 587 588 589Additional examples of proxies and reverse proxies can be found in 590the `Twisted web examples <../examples/index.html>`_ 591 592 593 594 595 596Advanced Configuration 597---------------------- 598 599 600 601Non-trivial configurations of Twisted Web are achieved with Python 602configuration files. This is a Python snippet which builds up a 603variable called application. Usually, 604the ``twisted.application.strports.service`` function will be used to build a 605service instance that will be used to make the application listen on a TCP port 606(80, in case direct web serving is desired), with the listener being 607a :py:class:`twisted.web.server.Site` . The resulting file 608can then be run with ``twistd -y`` . Alternatively a reactor object can be used directly to make 609a runnable script. 610 611 612 613 614The ``Site`` will wrap a ``Resource`` object -- the 615root. 616 617 618 619 620 621.. code-block:: python 622 623 624 from twisted.application import internet, service, strports 625 from twisted.web import static, server 626 627 root = static.File("/var/www/htdocs") 628 application = service.Application('web') 629 site = server.Site(root) 630 sc = service.IServiceCollection(application) 631 i = strports.service("tcp:80", site) 632 i.setServiceParent(sc) 633 634 635 636 637Most advanced configurations will be in the form of tweaking the 638root resource object. 639 640 641 642 643 644Adding Children 645~~~~~~~~~~~~~~~ 646 647 648 649Usually, the root's children will be based on the filesystem's contents. 650It is possible to override the filesystem by explicit ``putChild`` 651methods. 652 653 654 655 656Here are two examples. The first one adds a ``/doc`` child 657to serve the documentation of the installed packages, while the second 658one adds a ``cgi-bin`` directory for CGI scripts. 659 660 661 662 663 664.. code-block:: python 665 666 667 from twisted.internet import reactor, endpoints 668 from twisted.web import static, server 669 670 root = static.File("/var/www/htdocs") 671 root.putChild(b"doc", static.File("/usr/share/doc")) 672 endpoint = endpoints.TCP4ServerEndpoint(reactor, 80) 673 endpoint.listen(server.Site(root)) 674 reactor.run() 675 676 677 678 679 680.. code-block:: python 681 682 683 from twisted.internet import reactor, endpoints 684 from twisted.web import static, server, twcgi 685 686 root = static.File("/var/www/htdocs") 687 root.putChild(b"cgi-bin", twcgi.CGIDirectory("/var/www/cgi-bin")) 688 endpoint = endpoints.TCP4ServerEndpoint(reactor, 80) 689 endpoint.listen(server.Site(root)) 690 reactor.run() 691 692 693 694 695 696Modifying File Resources 697~~~~~~~~~~~~~~~~~~~~~~~~ 698 699 700 701``File`` resources, be they root object or children 702thereof, have two important attributes that often need to be 703modified: ``indexNames`` 704and ``processors`` . ``indexNames`` determines which 705files are treated as "index files" -- served up when a directory 706is rendered. ``processors`` determine how certain file 707extensions are treated. 708 709 710 711 712Here is an example for both, creating a site where all ``.rpy`` 713extensions are Resource Scripts, and which renders directories by 714searching for a ``index.rpy`` file. 715 716 717 718 719 720.. code-block:: python 721 722 723 from twisted.application import internet, service, strports 724 from twisted.web import static, server, script 725 726 root = static.File("/var/www/htdocs") 727 root.indexNames=['index.rpy'] 728 root.processors = {'.rpy': script.ResourceScript} 729 application = service.Application('web') 730 sc = service.IServiceCollection(application) 731 site = server.Site(root) 732 i = strports.service("tcp:80", site) 733 i.setServiceParent(sc) 734 735 736 737 738``File`` objects also have a method called ``ignoreExt`` . 739This method can be used to give extension-less URLs to users, so that 740implementation is hidden. Here is an example: 741 742 743 744 745 746.. code-block:: python 747 748 749 from twisted.application import internet, service, strports 750 from twisted.web import static, server, script 751 752 root = static.File("/var/www/htdocs") 753 root.ignoreExt(".rpy") 754 root.processors = {'.rpy': script.ResourceScript} 755 application = service.Application('web') 756 sc = service.IServiceCollection(application) 757 site = server.Site(root) 758 i = strports.service("tcp:80", site) 759 i.setServiceParent(sc) 760 761 762 763 764Now, a URL such as ``/foo`` might be served from a Resource 765Script called ``foo.rpy`` , if no file by the name of ``foo`` 766exists. 767 768 769``File`` objects will try to automatically determine the Content-Type and Content-Encoding headers. 770There is a small set of known mime types and encodings which augment the default mime types provided by the Python standard library `mimetypes`. 771You can always modify the content type and encoding mappings by manipulating the instance variables. 772 773For example to recognize WOFF File Format 2.0 and set the right Content-Type header you can modify the `contentTypes` member of an instance:: 774 775.. code-block:: python 776 777 778 from twisted.application import internet, service, strports 779 from twisted.web import static, server, script 780 781 root = static.File("/srv/fonts") 782 783 root.contentTypes[".woff2"] = "application/font-woff2" 784 785 application = service.Application('web') 786 sc = service.IServiceCollection(application) 787 site = server.Site(root) 788 i = strports.service("tcp:80", site) 789 i.setServiceParent(sc) 790 791 792 793Virtual Hosts 794~~~~~~~~~~~~~ 795 796 797 798Virtual hosting is done via a special resource, that should be used 799as the root resource 800-- ``NameVirtualHost`` . ``NameVirtualHost`` has an 801attribute named ``default`` , which holds the default 802website. If a different root for some other name is desired, 803the ``addHost`` method should be called. 804 805 806 807 808 809.. code-block:: python 810 811 812 from twisted.application import internet, service, strports 813 from twisted.web import static, server, vhost, script 814 815 root = vhost.NameVirtualHost() 816 817 # Add a default -- htdocs 818 root.default=static.File("/var/www/htdocs") 819 820 # Add a simple virtual host -- foo.com 821 root.addHost("foo.com", static.File("/var/www/foo")) 822 823 # Add a simple virtual host -- bar.com 824 root.addHost("bar.com", static.File("/var/www/bar")) 825 826 # The "baz" people want to use Resource Scripts in their web site 827 baz = static.File("/var/www/baz") 828 baz.processors = {'.rpy': script.ResourceScript} 829 baz.ignoreExt('.rpy') 830 root.addHost('baz', baz) 831 832 application = service.Application('web') 833 sc = service.IServiceCollection(application) 834 site = server.Site(root) 835 i = strports.service("tcp:80", site) 836 i.setServiceParent(sc) 837 838 839 840 841 842Advanced Techniques 843~~~~~~~~~~~~~~~~~~~ 844 845 846 847Since the configuration is a Python snippet, it is possible to 848use the full power of Python. Here are some simple examples: 849 850 851 852 853 854.. code-block:: python 855 856 857 # No need for configuration of virtual hosts -- just make sure 858 # a directory /var/vhosts/<vhost name> exists: 859 from twisted.web import vhost, static, server 860 from twisted.application import internet, service, strports 861 862 root = vhost.NameVirtualHost() 863 root.default = static.File("/var/www/htdocs") 864 for dir in os.listdir("/var/vhosts"): 865 root.addHost(dir, static.File(os.path.join("/var/vhosts", dir))) 866 867 application = service.Application('web') 868 sc = service.IServiceCollection(application) 869 site = server.Site(root) 870 i = strports.service("tcp:80", site) 871 i.setServiceParent(sc) 872 873 874 875 876 877.. code-block:: python 878 879 880 # Determine ports we listen on based on a file with numbers: 881 from twisted.web import vhost, static, server 882 from twisted.application import internet, service 883 884 root = static.File("/var/www/htdocs") 885 886 site = server.Site(root) 887 application = service.Application('web') 888 serviceCollection = service.IServiceCollection(application) 889 890 with open("/etc/web/ports") as f: 891 for num in map(int, f.read().split()): 892 serviceCollection.addCollection( 893 strports.service("tcp:{}".format(num), site) 894 ) 895 896 897 898 899 900 901Running a Twisted Web Server 902---------------------------- 903 904 905 906In many cases, you'll end up repeating common usage patterns of 907twisted.web. In those cases you'll probably want to use Twisted's 908pre-configured web server setup. 909 910 911 912 913The easiest way to run a Twisted Web server is with the Twisted Daemonizer. 914For example, this command will run a web server which serves static files from 915a particular directory: 916 917 918 919 920 921.. code-block:: console 922 923 924 % twistd web --path /path/to/web/content 925 926 927 928 929If you just want to serve content from your own home directory, the 930following will do: 931 932 933 934 935 936.. code-block:: console 937 938 939 % twistd web --path ~/public_html/ 940 941 942 943 944You can stop the server at any time by going back to the directory you 945started it in and running the command: 946 947 948 949 950 951.. code-block:: console 952 953 954 % kill `cat twistd.pid` 955 956 957 958 959Some other configuration options are available as well: 960 961 962 963 964 965 966- ``--listen`` : Specify the port for the web 967 server to listen on. This defaults to tcp:8080. 968- ``--logfile`` : Specify the path to the 969 log file. 970- ``--add-header``: Specify additional headers to be served with every response. 971 These are formatted like ``--add-header "HeaderName: HeaderValue"``. 972 973 974 975 976 977The full set of options that are available can be seen with: 978 979 980 981 982 983.. code-block:: console 984 985 986 % twistd web --help 987 988 989 990 991 992Serving Flat HTML 993~~~~~~~~~~~~~~~~~ 994 995 996 997Twisted Web serves flat HTML files just as it does any other flat file. 998 999 1000 1001.. _web-howto-using-twistedweb-resourcescripts: 1002 1003 1004 1005 1006 1007 1008 1009 1010Resource Scripts 1011~~~~~~~~~~~~~~~~ 1012 1013 1014 1015A Resource script is a Python file ending with the extension ``.rpy`` , which is required to create an instance of a (subclass of a) :py:class:`twisted.web.resource.Resource` . 1016 1017 1018 1019 1020Resource scripts have 3 special variables: 1021 1022 1023 1024 1025 1026 1027- ``__file__`` : The name of the .rpy file, including the full path. This variable is automatically defined and present within the namespace. 1028- ``registry`` : An object of class :py:class:`static.Registry <twisted.web.static.Registry>` . It can be used to access and set persistent data keyed by a class. 1029- ``resource`` : The variable which must be defined by the script and set to the resource instance that will be used to render the page. 1030 1031 1032 1033 1034 1035A very simple Resource Script might look like: 1036 1037 1038 1039 1040 1041.. code-block:: python 1042 1043 1044 from twisted.web import resource 1045 class MyGreatResource(resource.Resource): 1046 def render_GET(self, request): 1047 return b"<html>foo</html>" 1048 1049 resource = MyGreatResource() 1050 1051 1052 1053 1054A slightly more complicated resource script, which accesses some 1055persistent data, might look like: 1056 1057 1058 1059 1060 1061.. code-block:: python 1062 1063 1064 from twisted.web import resource 1065 from SillyWeb import Counter 1066 1067 counter = registry.getComponent(Counter) 1068 if not counter: 1069 registry.setComponent(Counter, Counter()) 1070 counter = registry.getComponent(Counter) 1071 1072 class MyResource(resource.Resource): 1073 def render_GET(self, request): 1074 counter.increment() 1075 output = "you are visitor {}".format(counter.getValue()) 1076 return output.encode("utf8") 1077 1078 resource = MyResource() 1079 1080 1081 1082 1083This is assuming you have the ``SillyWeb.Counter`` module, 1084implemented something like the following: 1085 1086 1087 1088 1089 1090.. code-block:: python 1091 1092 1093 class Counter: 1094 1095 def __init__(self): 1096 self.value = 0 1097 1098 def increment(self): 1099 self.value += 1 1100 1101 def getValue(self): 1102 return self.value 1103 1104 1105 1106 1107 1108Web UIs 1109~~~~~~~ 1110 1111 1112 1113 1114The `Nevow <https://launchpad.net/nevow>`_ framework, available as 1115part of the `Quotient <https://launchpad.net/quotient>`_ project, 1116is an advanced system for giving Web UIs to your application. Nevow uses Twisted Web but is 1117not itself part of Twisted. 1118 1119 1120 1121.. _web-howto-using-twistedweb-spreadablewebservers: 1122 1123 1124 1125 1126 1127 1128 1129 1130Spreadable Web Servers 1131~~~~~~~~~~~~~~~~~~~~~~ 1132 1133 1134 1135One of the most interesting applications of Twisted Web is the distributed webserver; multiple servers can all answer requests on the same port, using the :py:mod:`twisted.spread` package for "spreadable" computing. In two different directories, run the commands: 1136 1137 1138 1139 1140 1141.. code-block:: console 1142 1143 1144 % twistd web --user 1145 % twistd web --personal [other options, if you desire] 1146 1147 1148 1149 1150Once you're running both of these instances, go to ``http://localhost:8080/your_username.twistd/`` -- you will see the front page from the server you created with the ``--personal`` option. What's happening here is that the request you've sent is being relayed from the central (User) server to your own (Personal) server, over a PB connection. This technique can be highly useful for small "community" sites; using the code that makes this demo work, you can connect one HTTP port to multiple resources running with different permissions on the same machine, on different local machines, or even over the internet to a remote site. 1151 1152 1153 1154 1155 1156By default, a personal server listens on a UNIX socket in the owner's home 1157directory. The ``--listen`` option can be used to make 1158it listen on a different address, such as a TCP or SSL server or on a UNIX 1159server in a different location. If you use this option to make a personal 1160server listen on a different address, the central (User) server won't be 1161able to find it, but a custom server which uses the same APIs as the central 1162server might. Another use of the ``--listen`` option 1163is to make the UNIX server robust against system crashes. If the server 1164crashes and the UNIX socket is left on the filesystem, the personal server 1165will not be able to restart until it is removed. However, if ``--listen unix:/home/username/.twistd-web-pb:wantPID=1`` is 1166supplied when creating the personal server, then a lockfile will be used to 1167keep track of whether the server socket is in use and automatically delete 1168it when it is not. 1169 1170 1171 1172 1173 1174Serving PHP/Perl/CGI 1175~~~~~~~~~~~~~~~~~~~~ 1176 1177 1178 1179Everything related to CGI is located in 1180the ``twisted.web.twcgi`` , and it's here you'll find the 1181classes that you need to subclass in order to support the language of 1182your (or somebody elses) taste. You'll also need to create your own 1183kind of resource if you are using a non-unix operating system (such as 1184Windows), or if the default resources has wrong pathnames to the 1185parsers. 1186 1187 1188 1189 1190The following snippet is a .rpy that serves perl-files. Look at ``twisted.web.twcgi`` 1191for more examples regarding twisted.web and CGI. 1192 1193 1194 1195 1196 1197.. code-block:: python 1198 1199 1200 from twisted.web import static, twcgi 1201 1202 class PerlScript(twcgi.FilteredScript): 1203 filter = '/usr/bin/perl' # Points to the perl parser 1204 1205 resource = static.File("/perlsite") # Points to the perl website 1206 resource.processors = {".pl": PerlScript} # Files that end with .pl will be 1207 # processed by PerlScript 1208 resource.indexNames = ['index.pl'] 1209 1210 1211 1212 1213 1214Serving WSGI Applications 1215~~~~~~~~~~~~~~~~~~~~~~~~~ 1216 1217 1218 1219`WSGI <http://wsgi.org>`_ is the Web Server Gateway 1220Interface. It is a specification for web servers and application servers to 1221communicate with Python web applications. All modern Python web frameworks 1222support the WSGI interface. 1223 1224 1225 1226 1227The easiest way to get started with WSGI application is to use the twistd 1228command: 1229 1230 1231 1232 1233 1234.. code-block:: console 1235 1236 1237 % twistd -n web --wsgi=helloworld.application 1238 1239 1240 1241 1242This assumes that you have a WSGI application called application in 1243your helloworld module/package, which might look like this: 1244 1245 1246 1247 1248 1249.. code-block:: python 1250 1251 1252 def application(environ, start_response): 1253 """Basic WSGI Application""" 1254 start_response('200 OK', [('Content-type','text/plain')]) 1255 return [b'Hello World!'] 1256 1257 1258 1259 1260The above setup will be suitable for many applications where all that is 1261needed is to server the WSGI application at the site's root. However, for 1262greater control, Twisted provides support for using WSGI applications as 1263resources ``twisted.web.wsgi.WSGIResource`` . 1264 1265 1266 1267 1268Here is an example of a WSGI application being served as the root resource 1269for a site, in the following tac file: 1270 1271 1272 1273 1274 1275.. code-block:: python 1276 1277 1278 from twisted.web import server 1279 from twisted.web.wsgi import WSGIResource 1280 from twisted.python.threadpool import ThreadPool 1281 from twisted.internet import reactor 1282 from twisted.application import service, strports 1283 1284 # Create and start a thread pool, 1285 wsgiThreadPool = ThreadPool() 1286 wsgiThreadPool.start() 1287 1288 # ensuring that it will be stopped when the reactor shuts down 1289 reactor.addSystemEventTrigger('after', 'shutdown', wsgiThreadPool.stop) 1290 1291 def application(environ, start_response): 1292 """A basic WSGI application""" 1293 start_response('200 OK', [('Content-type','text/plain')]) 1294 return [b'Hello World!'] 1295 1296 # Create the WSGI resource 1297 wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, application) 1298 1299 # Hooks for twistd 1300 application = service.Application('Twisted.web.wsgi Hello World Example') 1301 server = strports.service('tcp:8080', server.Site(wsgiAppAsResource)) 1302 server.setServiceParent(application) 1303 1304 1305 1306 1307This can then be run like any other .tac file: 1308 1309 1310 1311 1312 1313.. code-block:: console 1314 1315 1316 % twistd -ny myapp.tac 1317 1318 1319 1320 1321Because of the synchronous nature of WSGI, each application call (for 1322each request) is called within a thread, and the result is written back to the 1323web server. For this, a ``twisted.python.threadpool.ThreadPool`` 1324instance is used. 1325 1326 1327 1328 1329 1330Using VHostMonster 1331~~~~~~~~~~~~~~~~~~ 1332 1333 1334 1335It is common to use one server (for example, Apache) on a site with multiple 1336names which then uses reverse proxy (in Apache, via ``mod_proxy`` ) to different 1337internal web servers, possibly on different machines. However, naive 1338configuration causes miscommunication: the internal server firmly believes it 1339is running on "internal-name:port" , and will generate URLs to that effect, 1340which will be completely wrong when received by the client. 1341 1342 1343 1344 1345While Apache has the ProxyPassReverse directive, it is really a hack 1346and is nowhere near comprehensive enough. Instead, the recommended practice 1347in case the internal web server is Twisted Web is to use VHostMonster. 1348 1349 1350 1351 1352From the Twisted side, using VHostMonster is easy: just drop a file named 1353(for example) ``vhost.rpy`` containing the following: 1354 1355 1356 1357 1358 1359.. code-block:: python 1360 1361 1362 from twisted.web import vhost 1363 resource = vhost.VHostMonsterResource() 1364 1365 1366 1367 1368Make sure the web server is configured with the correct processors 1369for the ``rpy`` extensions (the web server ``twistd web --path`` generates by default is so configured). 1370 1371 1372 1373 1374From the Apache side, instead of using the following ProxyPass directive: 1375 1376 1377 1378 1379 1380:: 1381 1382 1383 <VirtualHost ip-addr> 1384 ProxyPass / http://localhost:8538/ 1385 ServerName example.com 1386 </VirtualHost> 1387 1388 1389 1390 1391Use the following directive: 1392 1393 1394 1395 1396 1397:: 1398 1399 1400 <VirtualHost ip-addr> 1401 ProxyPass / http://localhost:8538/vhost.rpy/http/example.com:80/ 1402 ServerName example.com 1403 </VirtualHost> 1404 1405 1406 1407 1408Here is an example for Twisted Web's reverse proxy: 1409 1410 1411 1412 1413 1414.. code-block:: python 1415 1416 1417 from twisted.application import internet, service, strports 1418 from twisted.web import proxy, server, vhost 1419 vhostName = b'example.com' 1420 reverseProxy = proxy.ReverseProxyResource('internal', 8538, 1421 b'/vhost.rpy/http/'+vhostName+b'/') 1422 root = vhost.NameVirtualHost() 1423 root.addHost(vhostName, reverseProxy) 1424 site = server.Site(root) 1425 application = service.Application('web-proxy') 1426 sc = service.IServiceCollection(application) 1427 i = strports.service("tcp:80", site) 1428 i.setServiceParent(sc) 1429 1430 1431 1432 1433 1434Rewriting URLs 1435-------------- 1436 1437 1438 1439Sometimes it is convenient to modify the content of 1440the :py:class:`Request <twisted.web.server.Request>` object 1441before passing it on. Because this is most often used to rewrite 1442either the URL, the similarity to Apache's ``mod_rewrite`` 1443has inspired the :py:mod:`twisted.web.rewrite` 1444module. Using this module is done via wrapping a resource with 1445a :py:class:`twisted.web.rewrite.RewriterResource` which 1446then has rewrite rules. Rewrite rules are functions which accept a 1447request object, and possible modify it. After all rewrite rules run, 1448the child resolution chain continues as if the wrapped resource, 1449rather than the :py:class:`RewriterResource <twisted.web.rewrite.RewriterResource>` , was the child. 1450 1451 1452 1453 1454Here is an example, using the only rule currently supplied by Twisted 1455itself: 1456 1457 1458 1459 1460 1461.. code-block:: python 1462 1463 1464 default_root = rewrite.RewriterResource(default, rewrite.tildeToUsers) 1465 1466 1467 1468 1469This causes the URL ``/~foo/bar.html`` to be treated 1470like ``/users/foo/bar.html`` . If done after setting 1471default's ``users`` child to a :py:class:`distrib.UserDirectory <twisted.web.distrib.UserDirectory>` , it gives a 1472configuration similar to the classical configuration of web server, 1473common since the first NCSA servers. 1474 1475 1476 1477 1478 1479Knowing When We're Not Wanted 1480----------------------------- 1481 1482 1483 1484Sometimes it is useful to know when the other side has broken the connection. 1485Here is an example which does that: 1486 1487 1488 1489 1490 1491.. code-block:: python 1492 1493 1494 from twisted.web.resource import Resource 1495 from twisted.web import server 1496 from twisted.internet import reactor 1497 from twisted.python.util import println 1498 1499 1500 class ExampleResource(Resource): 1501 1502 def render_GET(self, request): 1503 request.write(b"hello world") 1504 d = request.notifyFinish() 1505 d.addCallback(lambda _: println("finished normally")) 1506 d.addErrback(println, "error") 1507 reactor.callLater(10, request.finish) 1508 return server.NOT_DONE_YET 1509 1510 resource = ExampleResource() 1511 1512 1513 1514 1515This will allow us to run statistics on the log-file to see how many users 1516are frustrated after merely 10 seconds. 1517 1518 1519 1520 1521 1522As-Is Serving 1523------------- 1524 1525 1526 1527Sometimes, you want to be able to send headers and status 1528directly. While you can do this with a :py:func:`ResourceScript <twisted.web.script.ResourceScript>` , an easier way is to 1529use :py:class:`ASISProcessor <twisted.web.static.ASISProcessor>` . 1530Use it by, for example, adding it as a processor for 1531the ``.asis`` extension. Here is a sample file: 1532 1533 1534 1535 1536 1537:: 1538 1539 1540 HTTP/1.0 200 OK 1541 Content-Type: text/html 1542 1543 Hello world 1544