通过HTTP请求访问python函数。
spineapi的Python项目详细描述
脊椎API
通过HTTP请求访问python函数。在
它简单、干净、使用方便。它也适用于jupyter笔记本电脑。在
1。设置api服务器
{str}1前提条件
- 确保可以通过端口3000访问服务器
- 有节点.js安装在服务器上
安装
$ git clone https://github.com/spineapi/spine-api
$ cd spine-api/server
$ npm install
Run server
^{pr2}$现在您可以通过http://YOUR_服务器_IP:3000。复制密码。在
安装
$ pip instal spineapi
将python脚本连接到服务器
1。导入库
fromspineapiimportConnection
2。定义您的功能
defhello_function(input):# ...# do something# ...returnoutput
3。指定name
和description
初始化项目
spine_connection=Connection(project_path="hello_project",project_name="My first project",description="Arithmetic operations",base_url="http://localhost:3000",passcode="xxxx-xxxx-xxxx-xxxxx",author="",# Optionallink=""# Optional)
4。注册您的功能
spine_connection.register_function(pathname='hello_function',function=hello_function,# ============ Optional ==================# Set True if you want to protect this APIrequiresAuth=False,authToken="xxx",# ========================================)
可以通过/api/hello_project/hello_function
访问该函数
^{str 1}5美元。运行
spine_connection.run()
就这样!现在可以通过httppost请求与ML模型通信。在
6。发送请求
Note您必须首先对请求数据运行JSON.stringify(input)
。在
constdata={input:JSON.stringify(YOUR_INPUT)// ============ Optional ==================// Required if the API is protectedauthToken:"xxx",// ========================================}
将请求正文发布到终结点。在
consturl='http://localhost:3000/api/hello_project/hello_function';fetch(url,{method:'POST',body:JSON.stringify(data),headers:newHeaders({'Content-Type':'application/json'})}).then(res=>res.json()).catch(error=>console.error('Error:',error)).then(response=>console.log('Success:',response));
许可证
- 项目
标签: