File: using-twistedweb.rst

package info (click to toggle)
twisted 25.5.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 20,560 kB
  • sloc: python: 203,171; makefile: 200; sh: 92; javascript: 36; xml: 31
file content (1544 lines) | stat: -rw-r--r-- 36,072 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544

:LastChangedDate: $LastChangedDate$
:LastChangedRevision: $LastChangedRevision$
:LastChangedBy: $LastChangedBy$

Configuring and Using the Twisted Web Server
============================================






Twisted Web Development
-----------------------
.. _web-howto-using-twistedweb-development:








Twisted Web serves Python objects that implement the interface
IResource.






.. image:: ../img/web-process.png





Main Concepts
~~~~~~~~~~~~~





- :ref:`Site Objects <web-howto-using-twistedweb-sites>` are responsible for
  creating ``HTTPChannel`` instances to parse the HTTP request,
  and begin the object lookup process. They contain the root Resource,
  the resource which represents the URL ``/`` on the site.
- :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.
- :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.
- :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.
- :ref:`Resource rendering <web-howto-using-twistedweb-rendering>` occurs when Twisted Web locates a leaf Resource object. A Resource can either return a response as bytes or write to the request object.
- :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.





The Twisted Web server is started through the Twisted Daemonizer, as in:





.. code-block:: console


    % twistd web





Site Objects
~~~~~~~~~~~~

.. _web-howto-using-twistedweb-sites:








Site objects serve as the glue between a port to listen for HTTP requests on, and a root Resource object.




When 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.




You can also create a ``Site`` instance by hand, passing
it a ``Resource`` object which will serve as the root of the
site:





.. code-block:: python


    from twisted.web import server, resource
    from twisted.internet import reactor, endpoints

    class Simple(resource.Resource):
        isLeaf = True
        def render_GET(self, request):
            return b"<html>Hello, world!</html>"

    site = server.Site(Simple())
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
    endpoint.listen(site)
    reactor.run()

By default Twisted Web will parse form submissions posted in HTTP request bodies into memory, which can use a lot of memory for large uploads.
If you want to implement your own parsing logic, you can disable Twisted Web's logic by passing ``parsePOSTFormSubmission=False`` to the ``Site()`` constructor.



Resource objects
~~~~~~~~~~~~~~~~

.. _web-howto-using-twistedweb-resources:








``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.




When 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.




During 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`` .




A Resource can know where it is in the URL tree by looking at ``request.prepath`` , a list of URL segment parts.




A Resource can know which path segments will be processed after it by looking at ``request.postpath`` .




If the URL ends in a slash, for example ``http://example.com/foo/bar/`` , the final URL segment will be empty. Resources can thus know if they were requested with or without a final slash.




Here is a simple Resource object:





.. code-block:: python


    from twisted.web.resource import Resource

    class Hello(Resource):
        isLeaf = True
        def getChild(self, name, request):
            if name == b'':
                return self
            return Resource.getChild(self, name, request)

        def render_GET(self, request):
            output = "Hello, world! I am located at {}.".format(request.prepath)
            return output.encode("utf8")

    resource = Hello()





Resource Trees
~~~~~~~~~~~~~~

.. _web-howto-using-twistedweb-trees:








Resources 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:





.. code-block:: python


    root = Hello()
    root.putChild(b'fred', Hello())
    root.putChild(b'bob', Hello())




If this root resource is served as the root of a Site instance, the following URLs will all be valid:





- ``http://example.com/``
- ``http://example.com/fred``
- ``http://example.com/bob``
- ``http://example.com/fred/``
- ``http://example.com/bob/``






.rpy scripts
~~~~~~~~~~~~

.. _web-howto-using-twistedweb-rpys:








Files 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.




An ``.rpy`` script must define a variable, ``resource`` , which is the Resource object that will render the request.




``.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:





.. code-block:: python


    from twisted.web.resource import Resource

    class MyResource(Resource):
        def render_GET(self, request):
            return b"<html>Hello, world!</html>"

    resource = MyResource()




However, 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:





.. code-block:: python


    import myresource

    ## Comment out this line when finished debugging
    reload(myresource)

    resource = myresource.MyResource()




Creating a Twisted Web server which serves a directory is easy:





.. code-block:: console


    % twistd -n web --path /Users/dsp/Sites





Resource rendering
~~~~~~~~~~~~~~~~~~

.. _web-howto-using-twistedweb-rendering:








Resource 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:





- Return a response as bytes
- 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)
- Request a ``Deferred`` , return ``server.NOT_DONE_YET`` , and call ``request.write("stuff")`` and ``request.finish()`` later, in a callback on the ``Deferred`` .







The :py:class:`Resource <twisted.web.resource.Resource>`
class, which is usually what one's Resource classes subclass, has a
convenient default implementation
of ``render`` . It will call a method
named ``self.render_METHOD``
where "METHOD" is whatever HTTP method was used to request this
resource. Examples: request_GET, request_POST, request_HEAD, and so
on. It is recommended that you have your resource classes
subclass :py:class:`Resource <twisted.web.resource.Resource>`
and implement ``render_METHOD`` methods as
opposed to ``render`` itself. Note that for
certain resources, ``request_POST = request_GET`` may be desirable in case one wants to process
arguments passed to the resource regardless of whether they used GET
(``?foo=bar&baz=quux`` , and so forth) or POST.






Request encoders
~~~~~~~~~~~~~~~~




When using a :py:class:`Resource <twisted.web.resource.Resource>` ,
one can specify wrap it using a
:py:class:`EncodingResourceWrapper <twisted.web.resource.EncodingResourceWrapper>`
and passing a list of encoder factories.  The encoder factories are
called when a request is processed and potentially return an encoder.
By default twisted provides
:py:class:`GzipEncoderFactory <twisted.web.server.GzipEncoderFactory>` which
manages standard gzip compression. You can use it this way:





.. code-block:: python


    from twisted.web.server import Site, GzipEncoderFactory
    from twisted.web.resource import Resource, EncodingResourceWrapper
    from twisted.internet import reactor, endpoints

    class Simple(Resource):
        isLeaf = True
        def render_GET(self, request):
            return b"<html>Hello, world!</html>"

    resource = Simple()
    wrapped = EncodingResourceWrapper(resource, [GzipEncoderFactory()])
    site = Site(wrapped)
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
    endpoint.listen(site)
    reactor.run()





Using compression on SSL served resources where the user can influence the
content can lead to information leak, so be careful which resources use
request encoders.





Note that only encoder can be used per request: the first encoder factory
returning an object will be used, so the order in which they are specified
matters.





Session
~~~~~~~

.. _web-howto-using-twistedweb-sessions:








HTTP 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.




Twisted 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.





.. image:: ../img/web-session.png





Proxies and reverse proxies
~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. _web-howto-using-twistedweb-proxies:








A proxy is a general term for a server that functions as an intermediary
between clients and other servers.




Twisted supports two main proxy variants: a :py:class:`Proxy <twisted.web.proxy.Proxy>` and a :py:class:`ReverseProxy <twisted.web.proxy.ReverseProxy>` .





Proxy
^^^^^



A proxy forwards requests made by a client to a destination server. Proxies
typically sit on the internal network for a client or out on the internet, and
have many uses, including caching, packet filtering, auditing, and circumventing
local access restrictions to web content.




Here is an example of a simple but complete web proxy:





.. code-block:: python


    from twisted.web import proxy, http
    from twisted.internet import reactor, endpoints

    class ProxyFactory(http.HTTPFactory):
        def buildProtocol(self, addr):
            return proxy.Proxy()

    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
    endpoint.listen(ProxyFactory())
    reactor.run()




With this proxy running, you can configure your web browser to use ``localhost:8080`` as a proxy. After doing so, when browsing the web
all requests will go through this proxy.




:py:class:`Proxy <twisted.web.proxy.Proxy>` inherits
from :py:class:`http.HTTPChannel <twisted.web.http.HTTPChannel>` . Each client
request to the proxy generates a :py:class:`ProxyRequest <twisted.web.proxy.ProxyRequest>` from the proxy to the destination
server on behalf of the client. ``ProxyRequest`` uses
a :py:class:`ProxyClientFactory <twisted.web.proxy.ProxyClientFactory>` to create
an instance of the :py:class:`ProxyClient <twisted.web.proxy.ProxyClient>`
protocol for the connection. ``ProxyClient`` inherits
from :py:class:`http.HTTPClient <twisted.web.http.HTTPClient>` . Subclass ``ProxyRequest`` to
customize the way requests are processed or logged.





ReverseProxyResource
^^^^^^^^^^^^^^^^^^^^



A reverse proxy retrieves resources from other servers on behalf of a
client. Reverse proxies typically sit inside the server's internal network and
are used for caching, application firewalls, and load balancing.




Here is an example of a basic reverse proxy:





.. code-block:: python


    from twisted.internet import reactor, endpoints
    from twisted.web import proxy, server

    site = server.Site(proxy.ReverseProxyResource('www.yahoo.com', 80, b''))
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8080)
    endpoint.listen(site)
    reactor.run()




With this reverse proxy running locally, you can
visit ``http://localhost:8080`` in your web browser, and the reverse
proxy will proxy your connection to ``www.yahoo.com``.




In this example we use ``server.Site`` to serve
a ``ReverseProxyResource`` directly. There is
also a ``ReverseProxy`` family of classes
in ``twisted.web.proxy`` mirroring those of the ``Proxy``
family:




Like ``Proxy`` , :py:class:`ReverseProxy <twisted.web.proxy.ReverseProxy>` inherits
from ``http.HTTPChannel`` . Each client request to the reverse proxy
generates a :py:class:`ReverseProxyRequest <twisted.web.proxy.ReverseProxyRequest>` to the destination
server. Like ``ProxyRequest`` , :py:class:`ReverseProxyRequest <twisted.web.proxy.ReverseProxyRequest>` uses a :py:class:`ProxyClientFactory <twisted.web.proxy.ProxyClientFactory>` to create an instance of
the :py:class:`ProxyClient <twisted.web.proxy.ProxyClient>` protocol for
the connection.




Additional examples of proxies and reverse proxies can be found in
the `Twisted web examples <../examples/index.html>`_





Advanced Configuration
----------------------



Non-trivial configurations of Twisted Web are achieved with Python
configuration files. This is a Python snippet which builds up a
variable called application. Usually,
the ``twisted.application.strports.service`` function will be used to build a
service instance that will be used to make the application listen on a TCP port
(80, in case direct web serving is desired), with the listener being
a :py:class:`twisted.web.server.Site` . The resulting file
can then be run with ``twistd -y`` . Alternatively a reactor object can be used directly to make
a runnable script.




The ``Site`` will wrap a ``Resource`` object -- the
root.





.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import static, server

    root = static.File("/var/www/htdocs")
    application = service.Application('web')
    site = server.Site(root)
    sc = service.IServiceCollection(application)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)




Most advanced configurations will be in the form of tweaking the
root resource object.





Adding Children
~~~~~~~~~~~~~~~



Usually, the root's children will be based on the filesystem's contents.
It is possible to override the filesystem by explicit ``putChild``
methods.




Here are two examples. The first one adds a ``/doc`` child
to serve the documentation of the installed packages, while the second
one adds a ``cgi-bin`` directory for CGI scripts.





.. code-block:: python


    from twisted.internet import reactor, endpoints
    from twisted.web import static, server

    root = static.File("/var/www/htdocs")
    root.putChild(b"doc", static.File("/usr/share/doc"))
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 80)
    endpoint.listen(server.Site(root))
    reactor.run()





.. code-block:: python


    from twisted.internet import reactor, endpoints
    from twisted.web import static, server, twcgi

    root = static.File("/var/www/htdocs")
    root.putChild(b"cgi-bin", twcgi.CGIDirectory("/var/www/cgi-bin"))
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 80)
    endpoint.listen(server.Site(root))
    reactor.run()





Modifying File Resources
~~~~~~~~~~~~~~~~~~~~~~~~



``File`` resources, be they root object or children
thereof, have two important attributes that often need to be
modified: ``indexNames``
and ``processors`` . ``indexNames`` determines which
files are treated as "index files" -- served up when a directory
is rendered. ``processors`` determine how certain file
extensions are treated.




Here is an example for both, creating a site where all ``.rpy``
extensions are Resource Scripts, and which renders directories by
searching for a ``index.rpy`` file.





.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import static, server, script

    root = static.File("/var/www/htdocs")
    root.indexNames=['index.rpy']
    root.processors = {'.rpy': script.ResourceScript}
    application = service.Application('web')
    sc = service.IServiceCollection(application)
    site = server.Site(root)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)




``File`` objects also have a method called ``ignoreExt`` .
This method can be used to give extension-less URLs to users, so that
implementation is hidden. Here is an example:





.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import static, server, script

    root = static.File("/var/www/htdocs")
    root.ignoreExt(".rpy")
    root.processors = {'.rpy': script.ResourceScript}
    application = service.Application('web')
    sc = service.IServiceCollection(application)
    site = server.Site(root)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)




Now, a URL such as ``/foo`` might be served from a Resource
Script called ``foo.rpy`` , if no file by the name of ``foo``
exists.


``File`` objects will try to automatically determine the Content-Type and Content-Encoding headers.
There is a small set of known mime types and encodings which augment the default mime types provided by the Python standard library `mimetypes`.
You can always modify the content type and encoding mappings by manipulating the instance variables.

For example to recognize WOFF File Format 2.0 and set the right Content-Type header you can modify the `contentTypes` member of an instance:

.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import static, server, script

    root = static.File("/srv/fonts")

    root.contentTypes[".woff2"] = "application/font-woff2"

    application = service.Application('web')
    sc = service.IServiceCollection(application)
    site = server.Site(root)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)



Virtual Hosts
~~~~~~~~~~~~~



Virtual hosting is done via a special resource, that should be used
as the root resource
-- ``NameVirtualHost`` . ``NameVirtualHost`` has an
attribute named ``default`` , which holds the default
website. If a different root for some other name is desired,
the ``addHost`` method should be called.





.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import static, server, vhost, script

    root = vhost.NameVirtualHost()

    # Add a default -- htdocs
    root.default=static.File("/var/www/htdocs")

    # Add a simple virtual host -- foo.com
    root.addHost("foo.com", static.File("/var/www/foo"))

    # Add a simple virtual host -- bar.com
    root.addHost("bar.com", static.File("/var/www/bar"))

    # The "baz" people want to use Resource Scripts in their web site
    baz = static.File("/var/www/baz")
    baz.processors = {'.rpy': script.ResourceScript}
    baz.ignoreExt('.rpy')
    root.addHost('baz', baz)

    application = service.Application('web')
    sc = service.IServiceCollection(application)
    site = server.Site(root)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)





Advanced Techniques
~~~~~~~~~~~~~~~~~~~



Since the configuration is a Python snippet, it is possible to
use the full power of Python. Here are some simple examples:





.. code-block:: python


    # No need for configuration of virtual hosts -- just make sure
    # a directory /var/vhosts/<vhost name> exists:
    from twisted.web import vhost, static, server
    from twisted.application import internet, service, strports

    root = vhost.NameVirtualHost()
    root.default = static.File("/var/www/htdocs")
    for dir in os.listdir("/var/vhosts"):
        root.addHost(dir, static.File(os.path.join("/var/vhosts", dir)))

    application = service.Application('web')
    sc = service.IServiceCollection(application)
    site = server.Site(root)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)





.. code-block:: python


    # Determine ports we listen on based on a file with numbers:
    from twisted.web import vhost, static, server
    from twisted.application import internet, service

    root = static.File("/var/www/htdocs")

    site = server.Site(root)
    application = service.Application('web')
    serviceCollection = service.IServiceCollection(application)

    with open("/etc/web/ports") as f:
        for num in map(int, f.read().split()):
            serviceCollection.addCollection(
                strports.service("tcp:{}".format(num), site)
            )






Running a Twisted Web Server
----------------------------



In many cases, you'll end up repeating common usage patterns of
twisted.web. In those cases you'll probably want to use Twisted's
pre-configured web server setup.




The easiest way to run a Twisted Web server is with the Twisted Daemonizer.
For example, this command will run a web server which serves static files from
a particular directory:





.. code-block:: console


    % twistd web --path /path/to/web/content




If you just want to serve content from your own home directory, the
following will do:





.. code-block:: console


    % twistd web --path ~/public_html/




You can stop the server at any time by going back to the directory you
started it in and running the command:





.. code-block:: console


    % kill `cat twistd.pid`




Some other configuration options are available as well:






- ``--listen`` : Specify the port for the web
  server to listen on.  This defaults to tcp:8080.
- ``--logfile`` : Specify the path to the
  log file.
- ``--add-header``: Specify additional headers to be served with every response.
  These are formatted like ``--add-header "HeaderName: HeaderValue"``.





The full set of options that are available can be seen with:





.. code-block:: console


    % twistd web --help





Serving Flat HTML
~~~~~~~~~~~~~~~~~



Twisted Web serves flat HTML files just as it does any other flat file.



.. _web-howto-using-twistedweb-resourcescripts:








Resource Scripts
~~~~~~~~~~~~~~~~



A 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` .




Resource scripts have 3 special variables:






- ``__file__`` : The name of the .rpy file, including the full path.  This variable is automatically defined and present within the namespace.
- ``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.
- ``resource`` : The variable which must be defined by the script and set to the resource instance that will be used to render the page.





A very simple Resource Script might look like:





.. code-block:: python


    from twisted.web import resource
    class MyGreatResource(resource.Resource):
        def render_GET(self, request):
            return b"<html>foo</html>"

    resource = MyGreatResource()




A slightly more complicated resource script, which accesses some
persistent data, might look like:





.. code-block:: python


    from twisted.web import resource
    from SillyWeb import Counter

    counter = registry.getComponent(Counter)
    if not counter:
       registry.setComponent(Counter, Counter())
    counter = registry.getComponent(Counter)

    class MyResource(resource.Resource):
        def render_GET(self, request):
            counter.increment()
            output = "you are visitor {}".format(counter.getValue())
            return output.encode("utf8")

    resource = MyResource()




This is assuming you have the ``SillyWeb.Counter`` module,
implemented something like the following:





.. code-block:: python


    class Counter:

        def __init__(self):
            self.value = 0

        def increment(self):
            self.value += 1

        def getValue(self):
            return self.value





Web UIs
~~~~~~~




The `Nevow <https://launchpad.net/nevow>`_ framework, available as
part of the `Quotient <https://launchpad.net/quotient>`_ project,
is an advanced system for giving Web UIs to your application. Nevow uses Twisted Web but is
not itself part of Twisted.



.. _web-howto-using-twistedweb-spreadablewebservers:








Spreadable Web Servers
~~~~~~~~~~~~~~~~~~~~~~



One 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:





.. code-block:: console


    % twistd web --user
    % twistd web --personal [other options, if you desire]




Once 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.





By default, a personal server listens on a UNIX socket in the owner's home
directory.  The ``--listen`` option can be used to make
it listen on a different address, such as a TCP or SSL server or on a UNIX
server in a different location.  If you use this option to make a personal
server listen on a different address, the central (User) server won't be
able to find it, but a custom server which uses the same APIs as the central
server might.  Another use of the ``--listen`` option
is to make the UNIX server robust against system crashes.  If the server
crashes and the UNIX socket is left on the filesystem, the personal server
will not be able to restart until it is removed.  However, if ``--listen unix:/home/username/.twistd-web-pb:wantPID=1`` is
supplied when creating the personal server, then a lockfile will be used to
keep track of whether the server socket is in use and automatically delete
it when it is not.





Serving PHP/Perl/CGI
~~~~~~~~~~~~~~~~~~~~



Everything related to CGI is located in
the ``twisted.web.twcgi`` , and it's here you'll find the
classes that you need to subclass in order to support the language of
your (or somebody elses) taste. You'll also need to create your own
kind of resource if you are using a non-unix operating system (such as
Windows), or if the default resources has wrong pathnames to the
parsers.




The following snippet is a .rpy that serves perl-files. Look at ``twisted.web.twcgi``
for more examples regarding twisted.web and CGI.





.. code-block:: python


    from twisted.web import static, twcgi

    class PerlScript(twcgi.FilteredScript):
        filter = '/usr/bin/perl' # Points to the perl parser

    resource = static.File("/perlsite") # Points to the perl website
    resource.processors = {".pl": PerlScript} # Files that end with .pl will be
                                              # processed by PerlScript
    resource.indexNames = ['index.pl']





Serving WSGI Applications
~~~~~~~~~~~~~~~~~~~~~~~~~



`WSGI <http://wsgi.org>`_ is the Web Server Gateway
Interface. It is a specification for web servers and application servers to
communicate with Python web applications. All modern Python web frameworks
support the WSGI interface.




The easiest way to get started with WSGI application is to use the twistd
command:





.. code-block:: console


    % twistd -n web --wsgi=helloworld.application




This assumes that you have a WSGI application called application in
your helloworld module/package, which might look like this:





.. code-block:: python


    def application(environ, start_response):
        """Basic WSGI Application"""
        start_response('200 OK', [('Content-type','text/plain')])
        return [b'Hello World!']




The above setup will be suitable for many applications where all that is
needed is to server the WSGI application at the site's root. However, for
greater control, Twisted provides support for using WSGI applications as
resources ``twisted.web.wsgi.WSGIResource`` .




Here is an example of a WSGI application being served as the root resource
for a site, in the following tac file:





.. code-block:: python


    from twisted.web import server
    from twisted.web.wsgi import WSGIResource
    from twisted.python.threadpool import ThreadPool
    from twisted.internet import reactor
    from twisted.application import service, strports

    # Create and start a thread pool,
    wsgiThreadPool = ThreadPool()
    wsgiThreadPool.start()

    # ensuring that it will be stopped when the reactor shuts down
    reactor.addSystemEventTrigger('after', 'shutdown', wsgiThreadPool.stop)

    def application(environ, start_response):
        """A basic WSGI application"""
        start_response('200 OK', [('Content-type','text/plain')])
        return [b'Hello World!']

    # Create the WSGI resource
    wsgiAppAsResource = WSGIResource(reactor, wsgiThreadPool, application)

    # Hooks for twistd
    application = service.Application('Twisted.web.wsgi Hello World Example')
    server = strports.service('tcp:8080', server.Site(wsgiAppAsResource))
    server.setServiceParent(application)




This can then be run like any other .tac file:





.. code-block:: console


    % twistd -ny myapp.tac




Because of the synchronous nature of WSGI, each application call (for
each request) is called within a thread, and the result is written back to the
web server. For this, a ``twisted.python.threadpool.ThreadPool``
instance is used.





Using VHostMonster
~~~~~~~~~~~~~~~~~~



It is common to use one server (for example, Apache) on a site with multiple
names which then uses reverse proxy (in Apache, via ``mod_proxy`` ) to different
internal web servers, possibly on different machines. However, naive
configuration causes miscommunication: the internal server firmly believes it
is running on "internal-name:port" , and will generate URLs to that effect,
which will be completely wrong when received by the client.




While Apache has the ProxyPassReverse directive, it is really a hack
and is nowhere near comprehensive enough. Instead, the recommended practice
in case the internal web server is Twisted Web is to use VHostMonster.




From the Twisted side, using VHostMonster is easy: just drop a file named
(for example) ``vhost.rpy`` containing the following:





.. code-block:: python


    from twisted.web import vhost
    resource = vhost.VHostMonsterResource()




Make sure the web server is configured with the correct processors
for the ``rpy`` extensions (the web server ``twistd web --path`` generates by default is so configured).




From the Apache side, instead of using the following ProxyPass directive:





::


    <VirtualHost ip-addr>
    ProxyPass / http://localhost:8538/
    ServerName example.com
    </VirtualHost>




Use the following directive:





::


    <VirtualHost ip-addr>
    ProxyPass / http://localhost:8538/vhost.rpy/http/example.com:80/
    ServerName example.com
    </VirtualHost>




Here is an example for Twisted Web's reverse proxy:





.. code-block:: python


    from twisted.application import internet, service, strports
    from twisted.web import proxy, server, vhost
    vhostName = b'example.com'
    reverseProxy = proxy.ReverseProxyResource('internal', 8538,
                                              b'/vhost.rpy/http/'+vhostName+b'/')
    root = vhost.NameVirtualHost()
    root.addHost(vhostName, reverseProxy)
    site = server.Site(root)
    application = service.Application('web-proxy')
    sc = service.IServiceCollection(application)
    i = strports.service("tcp:80", site)
    i.setServiceParent(sc)





Rewriting URLs
--------------



Sometimes it is convenient to modify the content of
the :py:class:`Request <twisted.web.server.Request>` object
before passing it on. Because this is most often used to rewrite
either the URL, the similarity to Apache's ``mod_rewrite``
has inspired the :py:mod:`twisted.web.rewrite`
module. Using this module is done via wrapping a resource with
a :py:class:`twisted.web.rewrite.RewriterResource` which
then has rewrite rules. Rewrite rules are functions which accept a
request object, and possible modify it. After all rewrite rules run,
the child resolution chain continues as if the wrapped resource,
rather than the :py:class:`RewriterResource <twisted.web.rewrite.RewriterResource>` , was the child.




Here is an example, using the only rule currently supplied by Twisted
itself:





.. code-block:: python


    default_root = rewrite.RewriterResource(default, rewrite.tildeToUsers)




This causes the URL ``/~foo/bar.html`` to be treated
like ``/users/foo/bar.html`` . If done after setting
default's ``users`` child to a :py:class:`distrib.UserDirectory <twisted.web.distrib.UserDirectory>` , it gives a
configuration similar to the classical configuration of web server,
common since the first NCSA servers.





Knowing When We're Not Wanted
-----------------------------



Sometimes it is useful to know when the other side has broken the connection.
Here is an example which does that:





.. code-block:: python


    from twisted.web.resource import Resource
    from twisted.web import server
    from twisted.internet import reactor
    from twisted.python.util import println


    class ExampleResource(Resource):

        def render_GET(self, request):
            request.write(b"hello world")
            d = request.notifyFinish()
            d.addCallback(lambda _: println("finished normally"))
            d.addErrback(println, "error")
            reactor.callLater(10, request.finish)
            return server.NOT_DONE_YET

    resource = ExampleResource()




This will allow us to run statistics on the log-file to see how many users
are frustrated after merely 10 seconds.





As-Is Serving
-------------



Sometimes, you want to be able to send headers and status
directly. While you can do this with a :py:func:`ResourceScript <twisted.web.script.ResourceScript>` , an easier way is to
use :py:class:`ASISProcessor <twisted.web.static.ASISProcessor>` .
Use it by, for example, adding it as a processor for
the ``.asis`` extension. Here is a sample file:





::


    HTTP/1.0 200 OK
    Content-Type: text/html

    Hello world