##################请求数据参数化
1、请求参数化中分为三种,其中第三种是最好用的
2、第二种使用规则:'http://115.28.108.130:5000/add/?a={a}&b={b}'.format(a=a,b=b)
3、第三种使用规则:f'http://115.28.108.130:5000/add/?a={a}&b={b}'
import random import requests def add(a, b): # url = 'http://115.28.108.130:5000/add/?a='+a+'&b='+b # url = 'http://115.28.108.130:5000/add/?a=%s&b=%s' % (a, b) # url = 'http://115.28.108.130:5000/add/?a=%(var_a)s&b=%(var_b)s' % dict(var_b=b, var_a=a) # url = 'http://115.28.108.130:5000/add/?a={}&b={}'.format(a,b) # url = 'http://115.28.108.130:5000/add/?a={a}&b={b}'.format(a=a,b=b) url = f'http://115.28.108.130:5000/add/?a={a}&b={b}' res = requests.request('GET', url) print(res.text) return res.text
1、可以将请求数据,通过参数传入,来提高函数的复用性
def add2(a, b): url = 'http://115.28.108.130:5000/add/' params = { 'a': a, 'b': b } res = requests.request('GET', url, params=params) print(res.text) add2(3, 50)