python__接口测试(python__ Interface test)

一种是使用requests库发送(post/get/head)请求

第一种:使用requests库发送get请求(要提前安装requests库)

import requests

# 输入URL地址,请求方法
URL = r"http://apis.juhe.cn/ip/ipNewV3"
# 输入请求的参数键值对
param = {"ip": "112.112.11.11", "key": "6df4403c2664b954ef63118c3f3df7d6"}
# 使用get方法发送请求
response = requests.get(url=URL, params=param)
# 返回数据的格式为json字符串
head = response.headers
res = response.json()
statu = response.status_code
# print 打印出head,返回json字符串,返回http状态码
print(f"请求头是{head}")
print(f"返回的json数据是{res}")
print(f"返回的状态码是{statu}")

第二种:使用urllib库

获取headers和http状态码

# 导包 urllib
import urllib.request
# 输入url地址
URL = r"http://www.baidu.com"
# 发送请求,打开地址,获取html页面
response = urllib.request.Request(url=URL)
html = urllib.request.urlopen(response)
# 打印响应头,状态码
print(html.headers)
print(html.getcode()) 
urllib发送post请求
# 输入URL地址,参数
URL = r"http://www.tuling123.com/openapi/api"
paras = {"key": "you", "info": "你好"}
# 首先要进行encode编码,然后发送post请求,打开地址,获取html页面
para = urllib.parse.urlencode(paras).encode("utf-8")
response = urllib.request.Request(url=URL, data=para)
html = urllib.request.urlopen(response)
# 打印出响应头,状态码
print(f"headers:{html.headers}")
print(f"status_code:{html.getcode()}")
总结:1.requests库比urllib用法简单一些2.python中get和post发送请求区别:get请求参数是params,post请求参数是data  get(url,params)  post(url,data)3.requests中获取http状态码:response.status_code  urllib 中获取http状态码:html.getcode()4.urllib 发送post请求时,首先要进行编码,即urllib.parse.urlencode(paras).encode("utf-8")
————————

One is to use the requests library to send (post / get / head) requests

The first is to use the requests library to send get requests (to install the requests Library in advance)

import requests

# 输入URL地址,请求方法
URL = r"http://apis.juhe.cn/ip/ipNewV3"
# 输入请求的参数键值对
param = {"ip": "112.112.11.11", "key": "6df4403c2664b954ef63118c3f3df7d6"}
# 使用get方法发送请求
response = requests.get(url=URL, params=param)
# 返回数据的格式为json字符串
head = response.headers
res = response.json()
statu = response.status_code
# print 打印出head,返回json字符串,返回http状态码
print(f"请求头是{head}")
print(f"返回的json数据是{res}")
print(f"返回的状态码是{statu}")

Second: use the urllib library

Get headers and HTTP status code

# 导包 urllib
import urllib.request
# 输入url地址
URL = r"http://www.baidu.com"
# 发送请求,打开地址,获取html页面
response = urllib.request.Request(url=URL)
html = urllib.request.urlopen(response)
# 打印响应头,状态码
print(html.headers)
print(html.getcode()) 
urllib发送post请求
# 输入URL地址,参数
URL = r"http://www.tuling123.com/openapi/api"
paras = {"key": "you", "info": "你好"}
# 首先要进行encode编码,然后发送post请求,打开地址,获取html页面
para = urllib.parse.urlencode(paras).encode("utf-8")
response = urllib.request.Request(url=URL, data=para)
html = urllib.request.urlopen(response)
# 打印出响应头,状态码
print(f"headers:{html.headers}")
print(f"status_code:{html.getcode()}")
总结:1.requests库比urllib用法简单一些2.python中get和post发送请求区别:get请求参数是params,post请求参数是data  get(url,params)  post(url,data)3.requests中获取http状态码:response.status_code  urllib 中获取http状态码:html.getcode()4.urllib 发送post请求时,首先要进行编码,即urllib.parse.urlencode(paras).encode("utf-8")