requests模块介绍:http://docs.python-requests.org/zh_CN/latest/index.html
#!/usr/bin/env python# -*- coding: utf-8 -*-# @File : module_requests.py# @Author: jing# @Date : 2019/1/24# @Desc :# -----requests模块------介绍# 1、安装requests模块# 打开cmd进入命令行模式—>输入pip insall requests—>回车# 2、打开pycharm#导入 requests库import requests#登录接口地址login_url='http://192.168.135.128:9999/futureloan/mvc/api/member/login'#登录接口的参数,且请求的参数一般是放到字典里面login_data={'mobilephone':'13417467890','pwd':'123456'}#发起一个get请求,并返回一个响应体login_response=requests.get(login_url,login_data)print('响应体是:',login_response)# 响应体是:# Response里面包含:响应头headers,响应正文text,状态码status_codeprint('登录的响应头是:',login_response.headers) # 头是以字典格式存储print('登录的响应正文是:',login_response.text)print('登录的响应状态码是:',login_response.status_code)print('获取json格式的响应正文:',login_response.json())print('登录成功,返回的cookie是:',login_response.cookies)print(login_response.cookies['JSESSIONID'])#cookies在取值时类似字典,用key# 登录的响应头是: {'Content-Type': 'application/json;charset=UTF-8', 'Date': 'Tue, 01 Jan 2019 04:26:06 GMT', 'Set-Cookie': 'JSESSIONID=7486AD63E6D6B00473F1492956596BF7; Path=/futureloan/; HttpOnly, rememberMe=deleteMe; Path=/futureloan; Max-Age=0; Expires=Mon, 31-Dec-2018 04:26:06 GMT', 'Server': 'Apache-Coyote/1.1', 'Transfer-Encoding': 'chunked'}# 登录的响应正文是: {"status":1,"code":"10001","data":null,"msg":"登录成功"}# 登录的响应状态码是: 200# 获取json格式的响应正文: {'status': 1, 'data': None, 'msg': '登录成功', 'code': '10001'}# 登录成功,返回的cookie是: ]># 347D82692A898B8223BED33D2D0F8B77# request 里面包含:URL,请求方式,请求正文,请求头print('登录的请求地址是:',login_response.request.url)print('登录的请求头是:',login_response.request.headers)print('登录的请求正文是:',login_response.request.body)# 登录的请求地址是: http://192.168.135.128:9999/futureloan/mvc/api/member/login?mobilephone=13417467890&pwd=123456# 登录的请求头是: {'Accept-Encoding': 'gzip, deflate', 'User-Agent': 'python-requests/2.21.0', 'Accept': '*/*', 'Connection': 'keep-alive'}# 登录的请求正文是: None-------------get请求,参数附在url后面,没有请求体body#发起一个post请求import requestsmsg1='登录成功'responsesdata=requests.post(url=login_url,data=login_data)print('登录以json格式返回的响应正文是:',responsesdata.json())print('post登录的请求地址是:',login_response.request.url)print('post登录的请求正文是:',login_response.request.body)#判断接口执行结果是否通过if responsesdata.json()['msg']=='登录成功': print('通过')