| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
This is a Twisted Web Server with Named-Based Virtual Host Support. |
| 6 |
|
| 7 |
Usage: |
| 8 |
$ sudo twistd -ny rootscript.py |
| 9 |
|
| 10 |
Note: You need to edit your hosts file for this example |
| 11 |
to work. Need to add the following entry: |
| 12 |
|
| 13 |
127.0.0.1 example.com |
| 14 |
|
| 15 |
Then visit http://example.com/ with a web browser and compare the results to |
| 16 |
visiting http://localhost/. |
| 17 |
""" |
| 18 |
|
| 19 |
from twisted.application import internet, service |
| 20 |
from twisted.web import script, server, static, vhost |
| 21 |
|
| 22 |
default = static.Data(b"", "text/html") |
| 23 |
# Setting up vhost resource. |
| 24 |
default.putChild(b"vhost", vhost.VHostMonsterResource()) |
| 25 |
resource = vhost.NameVirtualHost() |
| 26 |
resource.default = default |
| 27 |
# Here we use /var/www/html/ as our root diretory for the web server, you can |
| 28 |
# change it to whatever directory you want. |
| 29 |
root = static.File("/var/www/html/") |
| 30 |
root.processors = {".rpy": script.ResourceScript} |
| 31 |
# addHost binds domain name example.com to our root resource. |
| 32 |
resource.addHost("example.com", root) |
| 33 |
|
| 34 |
# Setup Twisted Application. |
| 35 |
site = server.Site(resource) |
| 36 |
application = service.Application("vhost") |
| 37 |
sc = service.IServiceCollection(application) |
| 38 |
# Only the processes owned by the root user can listen @ port 80, change the |
| 39 |
# port number here if you don't want to run it as root. |
| 40 |
i = internet.TCPServer(80, site) |
| 41 |
i.setServiceParent(sc) |