Skip to content

Commit

Permalink
Fix examples so that they work on Python 3
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigc committed Sep 14, 2020
1 parent cc38a1c commit 9e71c8a
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 31 deletions.
6 changes: 3 additions & 3 deletions docs/core/howto/tutorial/listings/finger/finger20.tac
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ class UserStatusTree(resource.Resource):
self.service=service

# add a specific child for the path "RPC2"
self.putChild("RPC2", UserStatusXR(self.service))
self.putChild(b"RPC2", UserStatusXR(self.service))

# need to do this for resources at the root of the site
self.putChild("", self)
self.putChild(b"", self)

def _cb_render_GET(self, users, request):
userOutput = ''.join(["<li><a href=\"%s\">%s</a></li>" % (user, user)
Expand Down Expand Up @@ -209,7 +209,7 @@ class UserStatus(resource.Resource):
self.service = service

def _cb_render_GET(self, status, request):
request.write("""<html><head><title>%s</title></head>
request.write(b"""<html><head><title>%s</title></head>
<body><h1>%s</h1>
<p>%s</p>
</body></html>""" % (self.user, self.user, status))
Expand Down
4 changes: 1 addition & 3 deletions docs/web/examples/advogato.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
$ python advogato.py <name> <diary entry file>
"""

from __future__ import print_function

import sys
from getpass import getpass

Expand All @@ -21,7 +19,7 @@ class AddDiary:
def __init__(self, name, password):
self.name = name
self.password = password
self.proxy = Proxy('http://advogato.org/XMLRPC')
self.proxy = Proxy(b'http://advogato.org/XMLRPC')

def __call__(self, filename):
with open(filename) as f:
Expand Down
3 changes: 2 additions & 1 deletion docs/web/examples/dlpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

# The function downloads a page and saves it to a file, in this case, it saves
# the page to "foo".
downloadPage(sys.argv[1], "foo").addCallbacks(
url = sys.argv[1].encode("ascii")
downloadPage(url, "foo").addCallbacks(
lambda value:reactor.stop(),
lambda error:(println("an error occurred",error),reactor.stop()))
reactor.run()
3 changes: 2 additions & 1 deletion docs/web/examples/getpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from twisted.web.client import getPage
from twisted.python.util import println

getPage(sys.argv[1]).addCallbacks(
url = sys.argv[1].encode("ascii")
getPage(url).addCallbacks(
callback=lambda value:(println(value),reactor.stop()),
errback=lambda error:(println("an error occurred", error),reactor.stop()))
reactor.run()
7 changes: 3 additions & 4 deletions docs/web/examples/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
Don't forget the http:// when you type the web address!
"""

from __future__ import print_function

import sys
from pprint import pprint

Expand Down Expand Up @@ -48,10 +46,11 @@ def main(reactor, url):
"""
We create a custom UserAgent and send a GET request to a web server.
"""
userAgent = 'Twisted/%s (httpclient.py)' % (version.short(),)
url = url.encode("ascii")
userAgent = 'Twisted/{} (httpclient.py)'.format(version.short()).encode("ascii")
agent = Agent(reactor)
d = agent.request(
'GET', url, Headers({'user-agent': [userAgent]}))
b'GET', url, Headers({b'user-agent': [userAgent]}))
def cbResponse(response):
"""
Prints out the response returned by the web server.
Expand Down
2 changes: 0 additions & 2 deletions docs/web/examples/logging-proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
See also proxy.py for a simpler proxy example.
"""

from __future__ import print_function

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

Expand Down
2 changes: 1 addition & 1 deletion docs/web/examples/rootscript.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

default = static.Data('text/html', '')
# Setting up vhost resource.
default.putChild('vhost', vhost.VHostMonsterResource())
default.putChild(b'vhost', vhost.VHostMonsterResource())
resource = vhost.NameVirtualHost()
resource.default = default
# Here we use /var/www/html/ as our root diretory for the web server, you can
Expand Down
2 changes: 1 addition & 1 deletion docs/web/examples/silly-web.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
Then visit http://localhost:19988/.
"""

from twisted.internet import reactor, protocol
from twisted.internet import reactor
from twisted.web import server, distrib, static
from twisted.spread import pb

Expand Down
2 changes: 1 addition & 1 deletion docs/web/examples/web.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
'.epy': script.PythonScript,
'.rpy': script.ResourceScript,
}
root.putChild('vhost', vhost.VHostMonsterResource())
root.putChild(b'vhost', vhost.VHostMonsterResource())
site = server.Site(root)
reactor.listenTCP(1999, site)
reactor.run()
6 changes: 3 additions & 3 deletions docs/web/examples/webguard.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def getChild(self, path, request):


def render(self, request):
return "Authorized!"
return b"Authorized!"



Expand All @@ -51,10 +51,10 @@ def requestAvatar(self, avatarId, mind, *interfaces):

def main():
log.startLogging(sys.stdout)
checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe='blow')]
checkers = [InMemoryUsernamePasswordDatabaseDontUse(joe=b'blow')]
wrapper = guard.HTTPAuthSessionWrapper(
Portal(SimpleRealm(), checkers),
[guard.DigestCredentialFactory('md5', 'example.com')])
[guard.DigestCredentialFactory('md5', b'example.com')])
reactor.listenTCP(8889, server.Site(
resource = wrapper))
reactor.run()
Expand Down
2 changes: 0 additions & 2 deletions docs/web/examples/xmlrpc-debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
the raw XML response string from the server.
"""

from __future__ import print_function

from twisted.web.xmlrpc import Proxy
from twisted.web.xmlrpc import QueryFactory
from twisted.internet import reactor
Expand Down
2 changes: 0 additions & 2 deletions docs/web/examples/xmlrpcclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
The example will make an XML-RPC request to advogato.org and display the result.
"""

from __future__ import print_function

from twisted.web.xmlrpc import Proxy
from twisted.internet import reactor

Expand Down
12 changes: 6 additions & 6 deletions docs/web/howto/using-twistedweb.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ Resources can be arranged in trees using ``putChild`` . ``putChild`` puts a Reso
root = Hello()
root.putChild('fred', Hello())
root.putChild('bob', Hello())
root.putChild(b'fred', Hello())
root.putChild(b'bob', Hello())
Expand Down Expand Up @@ -271,7 +271,7 @@ An ``.rpy`` script must define a variable, ``resource`` , which is the Resource
class MyResource(Resource):
def render_GET(self, request):
return "<html>Hello, world!</html>"
return b"<html>Hello, world!</html>"
resource = MyResource()
Expand Down Expand Up @@ -389,7 +389,7 @@ manages standard gzip compression. You can use it this way:
class Simple(Resource):
isLeaf = True
def render_GET(self, request):
return "<html>Hello, world!</html>"
return b"<html>Hello, world!</html>"
resource = Simple()
wrapped = EncodingResourceWrapper(resource, [GzipEncoderFactory()])
Expand Down Expand Up @@ -667,7 +667,7 @@ one adds a ``cgi-bin`` directory for CGI scripts.
from twisted.web import static, server
root = static.File("/var/www/htdocs")
root.putChild("doc", static.File("/usr/share/doc"))
root.putChild(b"doc", static.File("/usr/share/doc"))
endpoint = endpoints.TCP4ServerEndpoint(reactor, 80)
endpoint.listen(server.Site(root))
reactor.run()
Expand All @@ -683,7 +683,7 @@ one adds a ``cgi-bin`` directory for CGI scripts.
from twisted.web import static, server, twcgi
root = static.File("/var/www/htdocs")
root.putChild("cgi-bin", twcgi.CGIDirectory("/var/www/cgi-bin"))
root.putChild(b"cgi-bin", twcgi.CGIDirectory("/var/www/cgi-bin"))
endpoint = endpoints.TCP4ServerEndpoint(reactor, 80)
endpoint.listen(server.Site(root))
reactor.run()
Expand Down
2 changes: 1 addition & 1 deletion docs/web/howto/web-development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ using the ``twisted-web`` Debian package, and will drop in:
% cat > /etc/local.d/99addmypackage.py
from mypackage import toplevel
default.putChild("mypackage", toplevel.Resource(file="foo/bar", color="blue"))
default.putChild(b"mypackage", toplevel.Resource(file="foo/bar", color="blue"))
^D
Expand Down

0 comments on commit 9e71c8a

Please sign in to comment.