环境spyder(python3.6)
代码:
from flask import Flask, request
import json
import sys
defaultencoding = 'utf-8'
import logging
def tt(name, age):
result_str = "%s今年%s岁" % (name, age)
return result_str
app = Flask(__name__)
@app.route("/test_1.0", methods=["GET"])
def check():
return_dict = {'return_code': '200', 'return_info': '处理成功', 'result': False}
if request.args is None:
return_dict['return_code'] = '5004'
return_dict['return_info'] = '请求参数为空'
quit()
return json.dumps(return_dict, ensure_ascii=False)
get_data = request.args.to_dict()
name = get_data.get('name')
age = get_data.get('age')
return_dict['result'] = tt(name, age)
return json.dumps(return_dict, ensure_ascii=False)
if __name__ == "__main__":
app.run("0.0.0.0", port=5000, debug=True)
错误:
File "E:/NetFramework/Python/app.py", line 43, in <module>
app.run("0.0.0.0", port=5000, debug=True)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\flask\app.py", line 938, in run
cli.show_server_banner(self.env, self.debug, self.name, False)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\flask\cli.py", line 629, in show_server_banner
click.echo(message)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\click\utils.py", line 260, in echo
file.write(message)
UnsupportedOperation: not writable
针对上述错误:定位到D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\click\utils.py的函数echo,将参数file=None,改成file=sys.stdout
再次运行出错:
File "E:/NetFramework/Python/app.py", line 43, in <module>
app.run("0.0.0.0", port=5000, debug=True)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\flask\app.py", line 938, in run
cli.show_server_banner(self.env, self.debug, self.name, False)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\flask\cli.py", line 636, in show_server_banner
' environment.', fg='red')
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\click\termui.py", line 420, in secho
return echo(style(text, **styles), file=file, nl=nl, err=err, color=color)
File "D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\click\utils.py", line 260, in echo
file.write(message)
UnsupportedOperation: not writable
针对上述错误:定位到D:\Soft-win10\Microsoft Visual Studio\Shared\Anaconda3_64\lib\site-packages\click\termui.py中的函数secho,将参数file=None,改成file=sys.stdout
修改后,运行成功!!!