| 1 |
# Copyright (c) Twisted Matrix Laboratories. |
| 2 |
# See LICENSE for details. |
| 3 |
|
| 4 |
""" |
| 5 |
This example demonstrates how to get host information from a request object. |
| 6 |
|
| 7 |
To test the script, copy report.rpy to any directory, |
| 8 |
let's say /var/www/html/. |
| 9 |
|
| 10 |
Now, start your Twist web server: |
| 11 |
$ twistd -n web --path /var/www/html/ |
| 12 |
|
| 13 |
Then visit http://127.0.0.1:8080/report.rpy in your web browser. |
| 14 |
""" |
| 15 |
|
| 16 |
from twisted.web.resource import Resource |
| 17 |
|
| 18 |
|
| 19 |
class ReportResource(Resource): |
| 20 |
def render_GET(self, request): |
| 21 |
path = request.path |
| 22 |
host = request.getHost().host |
| 23 |
port = request.getHost().port |
| 24 |
url = request.prePathURL() |
| 25 |
uri = request.uri |
| 26 |
secure = (request.isSecure() and "securely") or "insecurely" |
| 27 |
output = """ |
| 28 |
<HTML> |
| 29 |
<HEAD><TITLE>Welcome To Twisted Python Reporting</title></head> |
| 30 |
|
| 31 |
<BODY><H1>Welcome To Twisted Python Reporting</H1> |
| 32 |
<UL> |
| 33 |
<LI>The path to me is {path} |
| 34 |
<LI>The host I'm on is {host} |
| 35 |
<LI>The port I'm on is {port} |
| 36 |
<LI>I was accessed {secure} |
| 37 |
<LI>A URL to me is {url} |
| 38 |
<LI>My URI to me is {uri} |
| 39 |
</UL> |
| 40 |
</body> |
| 41 |
</html>""".format( |
| 42 |
path=path, host=host, port=port, secure=secure, url=url, uri=uri |
| 43 |
) |
| 44 |
|
| 45 |
return output.encode("utf8") |
| 46 |
|
| 47 |
|
| 48 |
resource = ReportResource() |