Je ne sais pas si quelqu'un prendrait autant de peine pour tester les appels GET et POST. J'ai pris le module Python Flask et écrit une fonction qui fait quelque chose de similaire à ce que @Robert a partagé.
from flask import Flask, request
app = Flask(__name__)
@app.route('/method', methods=['GET', 'POST'])
@app.route('/method/<wish>', methods=['GET', 'POST'])
def method_used(wish=None):
if request.method == 'GET':
if wish:
if wish in dir(request):
ans = None
s = "ans = str(request.%s)" % wish
exec s
return ans
else:
return 'This wish is not available. The following are the available wishes: %s' % [method for method in dir(request) if '_' not in method]
else:
return 'This is just a GET method'
else:
return "You are using POST"
Lorsque je lance ceci, cela suit:
C:\Python27\python.exe E:/Arindam/Projects/Flask_Practice/first.py
* Restarting with stat
* Debugger is active!
* Debugger PIN: 581-155-269
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Essayons maintenant quelques appels. J'utilise le navigateur.
http://127.0.0.1:5000/method
Ceci est juste une méthode GET
http://127.0.0.1:5000/method/NotCorrect
Ce souhait n'est pas disponible. Voici les souhaits disponibles: ['application', 'args', 'autorisation', 'blueprint', 'charset', 'close', 'cookies', 'data', 'date', 'endpoint', 'environ "," fichiers "," formulaire "," en-têtes "," hôte "," json "," méthode "," type MIME "," module "," chemin "," pragma "," plage "," référent ", 'schéma', 'peu profond', 'flux', 'url', 'valeurs']
http://127.0.0.1:5000/method/environ
{'wsgi.multiprocess': False, 'HTTP_COOKIE': 'csrftoken = YFKYYZl3DtqEJJBwUlap28bLG1T4Cyuq', 'SERVER_SOFTWARE': 'Werkzeug / 0.12.2', 'SCRIPT_NAME':'M_HET ':' MH '/ method / environ', 'SERVER_PROTOCOL': 'HTTP / 1.1', 'QUERY_STRING': '', 'werkzeug.server.shutdown':, 'HTTP_USER_AGENT': 'Mozilla / 5.0 (Windows NT 6.1; WOW64) AppleWebKit / 537.36 (KHTML, comme Gecko) Chrome / 54.0.2840.71 Safari / 537.36 ',' HTTP_CONNECTION ':' keep-alive ',' SERVER_NAME ':' 127.0.0.1 ',' REMOTE_PORT ': 49569,' wsgi.url_scheme ':' http ',' SERVER_PORT ':' 5000 ',' werkzeug.request ':,' wsgi.input ':,' HTTP_HOST ':' 127.0.0.1: 5000 ',' wsgi.multithread ': Faux,' HTTP_UPGRADE_INSECURE_REQUESTS ':' 1 ',' HTTP_ACCEPT ':' text / html, application / xhtml + xml, application / xml; q = 0.9, image / webp, /; q = 0,8 ',' wsgi.version ': (1, 0),' wsgi.run_once ': False,' wsgi.errors ':', mode 'w' à 0x0000000002042150>, 'REMOTE_ADDR': '127.0.0.1 ',' HTTP_ACCEPT_LANGUAGE ':' en-US, en; q = 0.8 ',' HTTP_ACCEPT_ENCODING ':' gzip, deflate, sdch, br '}