我不确定是否有人会花这么多痛苦来测试GET和POST调用 . 我使用了Python Flask模块并编写了一个类似于@Robert共享的函数 .
from flask import Flask, request
app = Flask(__name__)
@app.route('/method', methods=['GET', 'POST'])
@app.route('/method/', 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"
当我运行这个时,会出现以下情况:
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)
Now lets try some calls. I am using the browser.
这只是一个GET方法
这个愿望不可用 . 以下是可用的愿望:['application','args','authorization','blueprint','charset','close','cookies','data','date','endpoint','environ ','''','form','headers','host','json','method','mimetype','module','path','pragma','range','referrer', 'scheme','shallow','stream','url','values']
{'wsgi.multiprocess':虚假,'HTTP_COOKIE':'csrftoken=YFKYYZl3DtqEJJBwUlap28bLG1T4Cyuq','SERVER_SOFTWARE':'Werkzeug/0.12.2','SCRIPT_NAME':'', ' REQUEST_METHOD ': ' GET ', ' PATH_INFO ': ' /方法/ ENVIRON ', ' SERVER_PROTOCOL ': ' HTTP / 1.1 ', ' QUERY_STRING ': ' ', ' werkzeug.server.shutdown ': , ' HTTP_USER_AGENT ': '的Mozilla / 5.0(视窗NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,与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 ': ',mode 'w' at 0x0000000002042150>,'REMOTE_ADDR':52751 1,'HTTP_ACCEPT_LANGUAGE':'en-US,en;q=0.8','HTTP_ACCEPT_ENCODING':'gzip, deflate, sdch, br'}