Cette fois, en cliquant sur le lien, on obtient quelque chose. Le navigateur affiche
Les commandes sont passées sous forme d'URLs virtuelles.
L'URL http://monge.univ-mlv.fr:8889/haddock
renvoie une injure :
L'URL monge.univ-mlv.fr:8889/jurons/6
renvoie 6 injures ...
... et L'URL monge.univ-mlv.fr:8889/portrait
renvoie une image ...
En adaptant progressivement l'exemple donné sur la page d'accueil de Flask, on arrive au code suivant :
#!/usr/bin/python3
from flask import Flask
app = Flask(__name__)
jurons=open('jurons.txt',encoding='latin1').readlines()
from random import randint
@app.route('/portrait')
def portrait():
html = '''<html><body>
<h1>Le capitaine Haddock</h1>
<br>
<img src=%s></body></html>'''
return html % ('static/haddock.jpg',)
@app.route('/haddock')
def jure():
return jurons[randint(0,len(jurons))]
@app.route('/jurons/<int:n>')
def fulmine(n):
html = '''<html><body>
<h1 align="center">Les %d jurons demandés :</h1>
<br>
<br> %s </body/</html>'''
s = '\n'.join(['<br>'+jurons[randint(0,len(jurons))] for i in range(n)])
return html % (n,s)
@app.route('/')
def help():
return '''<html><body>Commandes :\n<p> /haddock (renvoie une injure)\n
<p>/jurons/n (renvoie n injures)\n
<p>/portrait (renvoie une image)</body></html>'''
if __name__ == '__main__':
app.run()