progval / Limnoria

A robust, full-featured, and user/programmer-friendly Python IRC bot, with many existing plugins.
https://docs.limnoria.net/
Other
621 stars 174 forks source link

httpserver: Add support for WSGI apps #1521

Open progval opened 2 years ago

progval commented 2 years ago

For example, with Flask:

from supybot import utils, plugins, ircutils, callbacks, httpserver
from supybot.commands import *
from supybot.i18n import PluginInternationalization

_ = PluginInternationalization('TestWsgi')

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

@app.route('/login/', methods=['POST', 'GET'])
def login():
    error = None
    if request.method == 'POST':
        if request.form['username'] == 'root' \
                and request.form['password'] == 'admin':
            return 'Hello root'
        else:
            return 'Error: Invalid username/password'
    # the code below is executed if the request method
    # was GET or the credentials were invalid
    return '''
        <form method="POST" action=".">
            <label for="username">Username:
                <input name="username" id="username" />
            </label>
            <label for="password">Password:
                <input name="password" id="password" type="password" />
            </label>
            <input type="Submit" />
        </form>
    '''

class TestWsgi(callbacks.Plugin):
    """Test Flask"""
    def __init__(self, irc):
        self.__parent = super(TestWsgi, self)
        callbacks.Plugin.__init__(self, irc)

        httpserver.hook('testwsgi', app)

    def die(self):
        self.__parent.die()
        httpserver.unhook('testwsgi')

Class = TestWsgi