Sophie

Sophie

distrib > Mageia > 6 > x86_64 > by-pkgid > 09408f3c1571cbb746da49a1d2f22702 > files > 3290

python-twisted-17.5.0-1.mga6.x86_64.rpm


:LastChangedDate: $LastChangedDate$
:LastChangedRevision: $LastChangedRevision$
:LastChangedBy: $LastChangedBy$

Other Request Bodies
====================





The previous example demonstrated how to accept the payload of
a ``POST`` carrying HTML form data.  What about ``POST``
requests with data in some other format?  Or even ``PUT`` requests?
Here is an example which demonstrates how to get *any* request body,
regardless of its format - using the request's
:api:`twisted.web.iweb.IRequest.content <content>` attribute.




The only significant difference between this example and the previous is that
instead of accessing ``request.args``
in ``render_POST`` , it
uses ``request.content`` to get the request's body
directly:





.. code-block:: python


    ...
        def render_POST(self, request):
            return '<html><body>You submitted: %s</body></html>' % (cgi.escape(request.content.read()),)




``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).




Here's the complete source for this example - again, almost identical to the
previous ``POST`` example, with
only ``render_POST`` changed:





.. code-block:: python


    from twisted.web.server import Site
    from twisted.web.resource import Resource
    from twisted.internet import reactor, endpoints

    import cgi

    class FormPage(Resource):
        def render_GET(self, request):
            return '<html><body><form method="POST"><input name="the-field" type="text" /></form></body></html>'

        def render_POST(self, request):
            return '<html><body>You submitted: %s</body></html>' % (cgi.escape(request.content.read()),)

    root = Resource()
    root.putChild("form", FormPage())
    factory = Site(root)
    endpoint = endpoints.TCP4ServerEndpoint(reactor, 8880)
    endpoint.listen(factory)
    reactor.run()