圆月山庄资源网 Design By www.vgjia.com

这篇文章主要介绍了python用requests实现http请求过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1. get

import requests

# 最简单的get请求
r = requests.get(url)
print(r.status_code)
print(r.json())

# url 中"username":"name", "password":"passw0rd"}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
r = requests.get(url, params=params, headers=headers)

# 下载
r = requests.get(url)
r.raise_for_status()
with open(target, 'wb') as f:
  for ch in r.iter_content(10000):
    result_file_size += f.write(ch)

2. post请求

data = {'name':'train', 'device':'CN0989'}
r = requests.post(url, json=data)

#上传
files = {
    "file": (os.path.basename(filepath), open(filepath, "rb"), "application/zip")
}
print('POST %s'%url)
with open(filepath, 'rb') as f:
  r = requests.post(url, files=files)

3. 登录

_session = requests.Session()

# login
url = '%s/login'%_basic_url
params = {"username":"admin", "password":"admin"}
headers = {'Content-Type':'application/x-www-form-urlencoded'}
r = _session.post(url, params=params, headers=headers)

#做其他请求
r = _session.get(url)

_session.close()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
python,requests,http请求

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com