Source

twisted-meetup / web.py

Full commit
import time, sys
from twisted.web import server, resource
from twisted.internet import reactor, protocol
from twisted.python import log
from twisted.words.protocols import irc
from twisted.web.client import Response


class Simple(resource.Resource):
    isLeaf = True
    users = {}

    def render_GET(self, request):
        return """
        <html>
          <ul>%s</ul>
          <form method="POST">
            <input type="text" name="username">
            <input type="text" name="status">
            <input type="submit">
          </form>
        </html>
        """ % (
            ''.join('<li>%s: %s</li>' % (k, v) for k, v in self.users.iteritems())
        )

    def render_POST(self, request):
        username = None
        status = None
        if 'username' in request.args:
            username = request.args['username'][0]
        if 'status' in request.args:
            status = request.args['status'][0]
        if (username and status):
            self.change_status(username, status)
        request.setResponseCode(302)
        request.setHeader('Location', '/')
        return ''

    def change_status(self, username, status):
        self.users[username] = status

if __name__ == '__main__':
    site = server.Site(Simple())
    reactor.listenTCP(8080, site)
    reactor.run()