当前位置 : 主页 > 网页制作 > xml >

2019-6-4 接口总结

来源:互联网 收集:自由互联 发布时间:2021-06-13
session总结 h = { ‘ User-Agent ‘ : ‘ Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome /73.0.3683.103 Safari/537.36 ‘ , } # 伪造请求头 # 修改头部数据后,需更新头部 s.headers.

session总结

h = {User-Agent:  Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 

(KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36,
     }  # 伪造请求头

# 修改头部数据后,需更新头部
s.headers.update(h)

print(s.headers)
print(s.cookies)  # <RequestsCookieJar[]>

# 更新cookies
c = requests.cookies.RequestsCookieJar()
c.set("sessionid", "ajsw3mapvwi3okaeuuv7u824jz2uwrqv")
c.set("csrftoken", 

"BeWVNETgkYg0BotgfeBpB6tOXL0bb9iI0rqXqx6tGVJEg6WIfdbPhUWfVNVzx3R9")
s.cookies.update(c)
print(s.cookies)

# requests.get()     # requests.request("get", url=)
# requests.post()    # requests.request("post", url=)
# requests.delete()  # requests.request("delete", url=)”
‘‘‘----------------------------------------------------------------‘‘‘
#2 response 对象
r = s.get("http://47.104.190.48:8000/xadmin/hello/fileimage/")
r11 = r.cookies
r22 = r.headers
r33 = r.status_code   #获取状态码
r44 = r.url
r55 = r.text  # 以文本方式打印body
# r66 = r.json()  # 转码json 什么时候用json,什么时候用text?
# html 中文乱码 r.content.decode("utf-8")
r66 = r.content.decode("utf-8")
# 返回图片用r.content
r77 = r.content   # 数据流
#html对象就用text,有乱码要解码,json对象用json()方法

print(type(r))   # <class ‘requests.models.Response‘>Response对象

a = {"a":""}  # str
print(type(a))   # <class ‘str‘>
b = 123
print(type(b))

# 清空cookies

s.cookies.clear_session_cookies()  #清空全部cookies
s.cookies.clear(name="xxx")    #清空指定cookie

s.close()  # unclose 警告

 

接口请求类型

1.application/x-www-form-urlencoded

2.application/json

3.multipart/form-data

4.text/xml

接口数据重复解决方案

import requests
import json

s = requests.session()
url = "http://www.example.com/"
#接口传递json,先定义字典
j = {"a": 111,
     "b": True}
#将字典换成list
body = [
           ("a", 1),
           ("a", 1),
           ("b", 2),
           ("c", json.dumps(j)),   #把字典转换长json
]

s.post(url, data=body)

 

1111

网友评论