码迷,mamicode.com
首页 > 编程语言 > 详细

python 使用 requests 做 http 请求

时间:2019-10-30 16:20:06      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:sts   user   ams   orm   col   ESS   encode   style   basic   

1. get

import requests

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

# url 中?key=value&key=value
r = requests.get(url, params=params)

# form 表单
params = {"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 请求

标签:sts   user   ams   orm   col   ESS   encode   style   basic   

原文地址:https://www.cnblogs.com/snow-backup/p/11765578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!