From ffdae7d32944d91208a2c0572a288960ed5e1c21 Mon Sep 17 00:00:00 2001 From: pbr4nzfkh <18879212807@163.com> Date: Sun, 17 Mar 2024 16:01:48 +0800 Subject: [PATCH] =?UTF-8?q?restful=20=E5=AE=A2=E6=88=B7=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 基本结构/042 restful/tf-35-request.py | 45 +++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 基本结构/042 restful/tf-35-request.py diff --git a/基本结构/042 restful/tf-35-request.py b/基本结构/042 restful/tf-35-request.py new file mode 100644 index 0000000..8fa98c1 --- /dev/null +++ b/基本结构/042 restful/tf-35-request.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +import requests +from cppy.cp_util import * + +# 查询资源,得到空列表 +url = 'http://127.0.0.1:5000//books' +response = requests.get(url) +print(response.json()) +time.sleep(2) + +# - 创建一个1号资源 +print('创建一个1号资源') +book_1 = {"id": 1, "title": "Python编程:从入门到实践", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/1' +response = requests.put(url,json=book_1) +time.sleep(2) + +# - 创建一个2号资源(修改testfilepaht变量) +print('创建一个2号资源') +testfilepath = testfilepath.replace('Prey.txt','Pride-and-Prejudice.txt') +book_2 = {"id": 2, "title": "深入浅出计算机组成原理", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/2' +response = requests.put(url,json=book_2) +time.sleep(2) + +# - 创建一个3号资源(修改testfilepaht变量)正好有3个文件 +print('创建一个3号资源') +testfilepath = testfilepath.replace('Pride-and-Prejudice.txt','test.txt') +book_3 = {"id": 3, "title": "算法导论", "content": testfilepath} +url = 'http://127.0.0.1:5000/books/3' +response = requests.put(url,json=book_3) +time.sleep(2) + +# - 查询资源,看到结果 +print('查询资源,看到结果') +url = 'http://127.0.0.1:5000//books' +response = requests.get(url) +print(response.json()) +time.sleep(2) + +# - 操作1号资源,得到词频 +print('操作1号资源,得到词频') +url = 'http://127.0.0.1:5000/books/1/word_frequency' +response = requests.get(url) +print_word_freqs(response.json()) \ No newline at end of file