Skip to content

Commit

Permalink
"Twisted Web in 60 seconds" uses UTF-8 HTML5.
Browse files Browse the repository at this point in the history
This includes several typo fixes as well, e.g. "StringIO" for what's
now "BytesIO".
  • Loading branch information
markrwilliams committed Oct 5, 2017
1 parent 7396141 commit 006f480
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 33 deletions.
11 changes: 6 additions & 5 deletions docs/web/howto/web-in-60/dynamic-content.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@ time:
class ClockPage(Resource):
isLeaf = True
def render_GET(self, request):
return (b"<html><body>" + time.ctime().encode("ascii") +
b"</body></html>")
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>" + time.ctime().encode('utf-8'))
Expand All @@ -100,7 +100,8 @@ children.

The ``render_GET`` method here will be called whenever the URI we
hook this resource up to is requested with the ``GET`` method. The byte
string it returns is what will be sent to the browser.
string it returns is what will be sent to the browser. In this case, that
byte string is an HTML 5 web page encoded as UTF-8.



Expand Down Expand Up @@ -156,8 +157,8 @@ Here's the code with no interruptions:
class ClockPage(Resource):
isLeaf = True
def render_GET(self, request):
return (b"<html><body>" + time.ctime().encode("ascii") +
b"</body></html>")
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>" + time.ctime().encode('utf-8'))
resource = ClockPage()
factory = Site(resource)
Expand Down
8 changes: 6 additions & 2 deletions docs/web/howto/web-in-60/dynamic-dispatch.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ for the year passed to its initializer:
def render_GET(self, request):
cal = calendar(self.year)
return b"<html><body><pre>" + cal.encode("ascii") + b"</pre></body></html>"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body><pre>" + cal.encode('utf-8') + "</pre>")
Expand Down Expand Up @@ -128,14 +129,17 @@ basically like ``Calendar.getChild`` . Here's the full example code:
from calendar import calendar
class YearPage(Resource):
def __init__(self, year):
Resource.__init__(self)
self.year = year
def render_GET(self, request):
cal = calendar(self.year)
return b"<html><body><pre>" + cal.encode("ascii") + b"</pre></body></html>"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body><pre>" + cal.encode('utf-8') + "</pre>")
class Calendar(Resource):
def getChild(self, name, request):
Expand Down
3 changes: 2 additions & 1 deletion docs/web/howto/web-in-60/error-handling.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ complete code for this example:
def render_GET(self, request):
cal = calendar(self.year)
return b"<html><body><pre>" + cal.encode("ascii") + b"</pre></body></html>"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body><pre>" + cal.encode('utf-8') + "</pre>")
class Calendar(Resource):
def getChild(self, name, request):
Expand Down
27 changes: 18 additions & 9 deletions docs/web/howto/web-in-60/handling-posts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
:LastChangedRevision: $LastChangedRevision$
:LastChangedBy: $LastChangedBy$

Handling POSTs
==============
================
Handling POSTs
================



Expand Down Expand Up @@ -60,7 +61,9 @@ respond to ``GET`` requests with a static HTML form:
class FormPage(Resource):
def render_GET(self, request):
return b'<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"<form><input name='the-field' type='text'></form>")
Expand All @@ -78,9 +81,11 @@ method will allow it to accept ``POST`` requests:
...
def render_POST(self, request):
args = request.args[b"the-field"][0].decode("ascii")
args = request.args[b"the-field"][0].decode("utf-8")
escapedArgs = cgi.escape(args)
return b'<html><body>You submitted: ' + escapedArgs.encode("ascii") + b'</body></html>'
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"You submitted: " + escapedArgs.encode('utf-8'))
Expand All @@ -89,7 +94,7 @@ The main thing to note here is the use
of ``request.args`` . This is a dictionary-like object that
provides access to the contents of the form. The keys in this
dictionary are the names of inputs in the form. Each value is a list
containing strings (since there can be multiple inputs with the same
containing bytes objects (since there can be multiple inputs with the same
name), which is why we had to extract the first element to pass
to ``cgi.escape`` . ``request.args`` will be
populated from form contents whenever a ``POST`` request is
Expand Down Expand Up @@ -145,12 +150,16 @@ Here's the complete source for the example:
class FormPage(Resource):
def render_GET(self, request):
return b'<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"<form method='POST'><input name='the-field'></form>")
def render_POST(self, request):
args = request.args[b"the-field"][0].decode("ascii")
args = request.args[b"the-field"][0].decode("utf-8")
escapedArgs = cgi.escape(args)
return b'<html><body>You submitted: ' + escapedArgs.encode("ascii") + b'</body></html>'
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"You submitted: " + escapedArgs.encode('utf-8'))
root = Resource()
root.putChild(b"form", FormPage())
Expand Down
26 changes: 15 additions & 11 deletions docs/web/howto/web-in-60/other-request-bodies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ directly:
...
def render_POST(self, request):
content = request.content.read().decode("ascii")
content = request.content.read().decode("utf-8")
escapedContent = cgi.escape(content)
return (b'<html><body>You submitted: ' +
escapedContent.encode("ascii") +
b'</body></html>')
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"You submitted: " +
escapedContent.encode("utf-8"))
``request.content`` is a file-like object, so the
body is read from it. The exact type may vary, so avoid relying on non-file
methods you may find (such as ``getvalue`` when happens
to be a ``StringIO`` instance).
methods you may find (such as ``getvalue`` when ``request.content`` happens
to be a ``BytesIO`` instance).



Expand All @@ -71,14 +72,17 @@ only ``render_POST`` changed:
class FormPage(Resource):
def render_GET(self, request):
return b'<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"<form method='POST'><input name='the-field'></form>")
def render_POST(self, request):
content = request.content.read().decode("ascii")
content = request.content.read().decode("utf-8")
escapedContent = cgi.escape(content)
return (b'<html><body>You submitted: ' +
escapedContent.encode("ascii") +
b'</body></html>')
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"You submitted: " +
escapedContent.encode("utf-8"))
root = Resource()
root.putChild(b"form", FormPage())
Expand Down
4 changes: 3 additions & 1 deletion docs/web/howto/web-in-60/rpy-scripts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ put this code in it:
from twisted.web.resource import Resource
class ClockPage(Resource):
isLeaf = True
def render_GET(self, request):
return b"<html><body>" + time.ctime().encode("ascii") + b"</body></html>"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>" + time.ctime().encode('utf-8'))
resource = ClockPage()
Expand Down
13 changes: 9 additions & 4 deletions docs/web/howto/web-in-60/session-store.rst
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,10 @@ example above:
session = request.getSession()
counter = ICounter(session)
counter.value += 1
return b"Visit #" + str(counter.value).encode("ascii") + b" for you!"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"Visit #" + str(counter.value).encode("utf-8") +
b" for you!")
Expand Down Expand Up @@ -171,8 +173,11 @@ based on this example:
session = request.getSession()
counter = ICounter(session)
counter.value += 1
return b"Visit #" + str(counter.value).encode("ascii") + b" for you!"
return (b"<!DOCTYPE html><html><head><meta charset='utf-8'>"
b"<title></title></head><body>"
b"Visit #" + str(counter.value).encode("utf-8") +
b" for you!")
resource = CounterResource()
Expand Down

0 comments on commit 006f480

Please sign in to comment.