No estoy seguro si alguien se tomaría tantas molestias para probar las llamadas GET y POST. Tomé el módulo Python Flask y escribí una función que hace algo similar a lo que compartió @Robert.
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"
Cuando ejecuto esto, esto sigue:
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)
Ahora vamos a tratar algunas llamadas. Estoy usando el navegador.
http://127.0.0.1:5000/method
esto es sólo un método GET
http://127.0.0.1:5000/method/NotCorrect
Este deseo no está disponible. Los siguientes son los deseos disponibles: ['application', 'args', 'authorization', 'blueprint', 'charset', 'close', 'cookies', 'data', 'date', 'endpoint', 'environ ',' files ',' form ',' headers ',' host ',' json ',' method ',' mimetype ',' module ',' path ',' pragma ',' range ',' referrer ', 'esquema', 'poco profunda', 'url' 'corriente', 'valores']
http://127.0.0.1:5000/method/environ
{ 'wsgi.multiprocess': Falso, 'HTTP_COOKIE': '= csrftoken YFKYYZl3DtqEJJBwUlap28bLG1T4Cyuq', 'SERVER_SOFTWARE ':' Werkzeug/0.12.2 ',' SCRIPT_NAME ':' ',' REQUEST_METHOD ':' GET ',' PATH_INFO ':'/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, como 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 ': False,' 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': ', modo' w 'en 0x0000000002042150>,' REMOTE_ADDR ':' 127.0.0.1 ',' HTTP_ACCEPT_LANGUAGE ':' en-US, en; q = 0.8 ' , 'HTTP_ACCEPT_ENCODING': 'gzip, desinflar, sdch, sa'}
¿Lo quieres para registrar POST? –
Esto hace lo que quiere, creo. https://github.com/jar-o/dumdum No se requiere código – jar