python3中常见请求示例

#普通上传
files = {'file':open('test.txt','rb')}

#自定义文件名,文件类型、请求头
files = {'file':('test.png',open('test.png','rb'),'image/png')}

#多文件上传
files = [('file1',('test.txt',open('test.txt', 'rb'))),('file2', ('test.png', open('test.png', 'rb')))]
r = requests.post(url,files=files)

#流式上传
with open( 'test.txt' ) as f:
r = requests.post(url,data = f)

#将RequestsCookieJar转换成字典
c = requests.utils.dict_from_cookiejar(r.cookies)


#方法一:简单发送
# cookies = {"aaa":"bbb"}
# r = requests.get(url,cookies=cookies)
# print r.text

#方法二:复杂发送
s = requests.session()
c = requests.cookies.RequestsCookieJar()
c.set('c-name','c-value',path='/xxx/uuu',domain='.test.com')
s.cookies.update(c)

猜你喜欢

转载自www.cnblogs.com/NiceTime/p/9192289.html