在学习nacos的过程中,参考了一些很容易理解的文章,整理在这里
1.调用接口
python3 请求RESTful接口_Int93的博客-CSDN博客_python3 restful
import requests
import json
# #请求api地址
url="https://sug.so.360.cn/suggest"
# #请求参数
data={'callback':'suggest_so','encodein':'utf-8','encodeout':'utf-8','format':'json','fields':'word','word':'s'}
# #执行请求
response= requests.get(url,params=data)
# 查看请求执行的url
print(response.url)
# # 获得请求的内容:是个长得像json的字符串
print(response.text)
# #解析获取的json数据
data_json = json.loads(response.text[11:-1])
print(data_json['result'])
2.发布接口
Python发布微服务到注册中心Nacos_歪桃的博客-CSDN博客_python微服务注册中心
只需要看第一部分和第二部分,其中1.4参考github的Quick Start部分的流程更好
postman不一定要安装,踏实一个可视化调用接口的工具,直接代码调用接口或者在浏览器输入接口链接即可测试是否发布成功
如需安装:
postman基础教程_市井榴芒的博客-CSDN博客_postman 入门
from flask import Flask,jsonify,request
'''python发布restful服务实例
'''
#初始化。官方解释:http://flask.pocoo.org/docs/0.12/api/#flask.Flask
server = Flask(__name__)
#处理乱码
server.config['JSON_AS_ASCII']=False
# http://127.0.0.1:8085/simulation/analysis?name=胡涛
@server.route('/simulation/analysis',methods=['get'])
def demo_rest_get_request():
# 处理业务逻辑
name = request.args['name']
result = {"code":"200","msg":"SUCCES","data":{"name":name,"age":26,"job":"python"}}
return jsonify(result)
if __name__ == "__main__":
server.run(port=8085,debug=True)