Skip to content

Commit

Permalink
Fix on Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigc committed Sep 14, 2020
1 parent 5bdc7a0 commit 3c41e54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
9 changes: 4 additions & 5 deletions docs/web/examples/fortune.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ game that displays a random message from a database of quotations. You will need
to change the path of the fortune program if it's not in the "/usr/game"
directory.
To test the script, rename the file to fortune.rpy, and move it to any
To test the script, copy fortune.rpy to any
directory, let's say /var/www/html/
Now, start your Twisted web server:
Expand All @@ -22,7 +22,6 @@ And visit http://127.0.0.1:8080/fortune.rpy with a web browser.
from twisted.web.resource import Resource
from twisted.web import server
from twisted.internet import utils
from twisted.python import util

class FortuneResource(Resource):
"""
Expand All @@ -38,12 +37,12 @@ class FortuneResource(Resource):
Use L{utils.getProcessOutput}, which spawns a process and returns a
Deferred which fires with its output.
"""
request.write("<pre>\n")
request.write(b"<pre>\n")
deferred = utils.getProcessOutput(self.fortune)
deferred.addCallback(lambda s:
(request.write(s+"\n"), request.finish()))
(request.write(s+b"\n"), request.finish()))
deferred.addErrback(lambda s:
(request.write(str(s)), request.finish()))
(request.write(str(s).encode("utf8")), request.finish()))
return server.NOT_DONE_YET

resource = FortuneResource()
10 changes: 6 additions & 4 deletions docs/web/examples/hello.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ now = time.ctime()
d = '''\
<HTML><HEAD><TITLE>Hello Rpy</TITLE>
<H1>Hello World, It is Now %(now)s</H1>
<H1>Hello World, It is Now {now}</H1>
<UL>
''' % vars()
'''.format(now=now)

for i in range(10):
d += "<LI>%(i)s" % vars()
d += "<LI>{i}".format(i=i)

d += '''\
</UL>
</BODY></HTML>
'''

resource = static.Data(d, 'text/html')
data = d.encode("utf8")

resource = static.Data(data, 'text/html')
20 changes: 11 additions & 9 deletions docs/web/examples/report.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""
This example demonstrates how to get host information from a request object.
To test the script, rename the file to report.rpy, and move it to any directory,
To test the script, copy report.rpy to any directory,
let's say /var/www/html/.
Now, start your Twist web server:
Expand All @@ -25,20 +25,22 @@ class ReportResource(Resource):
url = request.prePathURL()
uri = request.uri
secure = (request.isSecure() and "securely") or "insecurely"
return ("""\
output = """
<HTML>
<HEAD><TITLE>Welcome To Twisted Python Reporting</title></head>
<BODY><H1>Welcome To Twisted Python Reporting</H1>
<UL>
<LI>The path to me is %(path)s
<LI>The host I'm on is %(host)s
<LI>The port I'm on is %(port)s
<LI>I was accessed %(secure)s
<LI>A URL to me is %(url)s
<LI>My URI to me is %(uri)s
<LI>The path to me is {path}
<LI>The host I'm on is {host}
<LI>The port I'm on is {port}
<LI>I was accessed {secure}
<LI>A URL to me is {url}
<LI>My URI to me is {uri}
</UL>
</body>
</html>""" % vars())
</html>""".format(path=path, host=host, port=port, secure=secure, url=url, uri=uri)

return output.encode("utf8")

resource = ReportResource()

0 comments on commit 3c41e54

Please sign in to comment.