如何制作一个简单的Python REST服务器和客户端?
我正在尝试制作尽可能简单的RESTAPI服务器和客户端,服务器和客户端都用Python编写并在同一台计算机上运行 从本教程中: 我将此用于服务器:如何制作一个简单的Python REST服务器和客户端?,python,rest,Python,Rest,我正在尝试制作尽可能简单的RESTAPI服务器和客户端,服务器和客户端都用Python编写并在同一台计算机上运行 从本教程中: 我将此用于服务器: # server.py from flask import Flask, jsonify app = Flask(__name__) tasks = [ { 'id': 1, 'title': u'Buy groceries', 'description': u'Milk, Cheese
# server.py
from flask import Flask, jsonify
app = Flask(__name__)
tasks = [
{
'id': 1,
'title': u'Buy groceries',
'description': u'Milk, Cheese, Pizza, Fruit, Tylenol',
'done': False
},
{
'id': 2,
'title': u'Learn Python',
'description': u'Need to find a good Python tutorial on the web',
'done': False
}
]
@app.route('/todo/api/v1.0/tasks', methods=['GET'])
def get_tasks():
return jsonify({'tasks': tasks})
if __name__ == '__main__':
app.run(debug=True)
如果从命令行运行此命令:
curl -i http://localhost:5000/todo/api/v1.0/tasks
我明白了:
HTTP/1.0 200 OK
Content-Type: application/json
Content-Length: 317
Server: Werkzeug/0.16.0 Python/3.6.9
Date: Thu, 05 Mar 2020 02:45:59 GMT
{
"tasks": [
{
"description": "Milk, Cheese, Pizza, Fruit, Tylenol",
"done": false,
"id": 1,
"title": "Buy groceries"
},
{
"description": "Need to find a good Python tutorial on the web",
"done": false,
"id": 2,
"title": "Learn Python"
}
]
}
很好,现在我的问题是,如何使用请求来编写Python脚本以获得相同的信息
我怀疑这是正确的想法:
# client.py
import requests
url = 'http://todo/api/v1.0/tasks'
response = requests.get(url,
# what goes here ??
)
print('response = ' + str(response))
但是,正如您从我的评论中看到的,我不确定如何设置请求的参数。get
我试图使用此SO帖子:
但是,作为指导原则,目前还不清楚如何根据消息更改调整格式
可以提供如何设置参数以传递到请求的简要说明。获取并建议必要的更改以使上述客户端示例正常工作?谢谢
---编辑--
我还可以提到的一点是,我很容易让客户端使用Postman进行工作,我只是不知道如何在Python中设置语法:
---编辑---
根据icedwater的以下回复,这是完整的、适用于客户的工作代码:
# client.py
import requests
import json
url = 'http://localhost:5000/todo/api/v1.0/tasks'
response = requests.get(url)
print(str(response))
print('')
print(json.dumps(response.json(), indent=4))
结果:
<Response [200]>
{
"tasks": [
{
"description": "Milk, Cheese, Pizza, Fruit, Tylenol",
"done": false,
"id": 1,
"title": "Buy groceries"
},
{
"description": "Need to find a good Python tutorial on the web",
"done": false,
"id": 2,
"title": "Learn Python"
}
]
}
{
“任务”:[
{
“描述”:“牛奶、奶酪、比萨饼、水果、泰诺”,
“完成”:错误,
“id”:1,
“标题”:“购买食品杂货”
},
{
“说明”:“需要在web上找到一个好的Python教程”,
“完成”:错误,
“id”:2,
“标题”:“学习Python”
}
]
}
来自帮助(requests.get)
:
使用实际的测试API尝试上面的代码。看起来url
没有正确定义以到达端点。你能核实一下吗?我对这篇文章中的细节有复杂的感觉。这是有用的,但也可能是压倒性的。考虑让问题变短,这样人们就可以看到它是什么:Pdata=requests.get(…).json()?Curl和Postman的工作不是证明了我只是缺少Python中必要的行吗?我现在休息一下,所以我不确定5000是从哪里来的,也许是某种默认值。在任何情况下,如果Curl和Postman可以接收消息,则必须能够生成一个Python脚本来接收消息。如果我将URL文本更改为“”以与服务器保持一致,则您的代码会显示错误“感谢@cdahms的响应,我很高兴这对您有所帮助。”。但我不确定是否有必要将其添加到答案中,因此我拒绝了编辑。
Help on function get in module requests.api:
get(url, params=None, **kwargs)
Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
import requests
import json
url = "https://postman-echo.com/get?testprop=testval"
response = requests.get(url)
print(json.dumps(response.json(), indent=4))