模块内的注释
def request(method, url, **kwargs): """Constructs and sends a :class:`Request <Request>`. :param method: method for the new :class:`Request` object. :param url: URL for the new :class:`Request` object. :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`. :param data: (optional) Dictionary or list of tuples ``[(key, value)]`` (will be form-encoded), bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`. :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`. :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`. :param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload. ``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')`` or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers to add for the file. :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth. :param timeout: (optional) How many seconds to wait for the server to send data before giving up, as a float, or a :ref:`(connect timeout, read timeout) <timeouts>` tuple. :type timeout: float or tuple :param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``. :type allow_redirects: bool :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy. :param verify: (optional) Either a boolean, in which case it controls whether we verify the server's TLS certificate, or a string, in which case it must be a path to a CA bundle to use. Defaults to ``True``. :param stream: (optional) if ``False``, the response content will be immediately downloaded. :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair. :return: :class:`Response <Response>` object :rtype: requests.Response
1、method:请求方式
requests.get、requests.post等方法实际是调用requests.request,将请求方式传入method参数(可做位置参数,请求方式会自动大写)
# 1. 方法
requests.get
requests.post
requests.put
requests.delete
...
requests.request(method='POST')
2、url:请求地址
3、params: 在URL中传递的参数
params = {'k1':'v1','k2':'v2'} ----- ?k1=v1&k2=v2
4、data 在请求体里传递的数据
requests.post(
...,
data={'user':'alex','pwd':'123'}
)
GET /index http1.1\r\nhost:c1.com\r\n\r\nuser=alex&pwd=123
django中发现【请求头:content-type: application/url-form-encod.....】,从request.post中取出data。data只能是字符串、数字和列表不能是字典。
5、json 在请求体里传递的数据
请求头: content-type: application/json
requests.post(
...,
json={'user':'alex','pwd':'123'}
)
GET /index http1.1\r\nhost:c1.com\r\nContent-Type:application/json\r\n\r\n{"user":"alex","pwd":123}
6、header:请求头
header={ 'Referer': 【上次访问url】, 'User-Agent':【客户端信息】}
7、Cookies
8、files 上传文件,文件对象或二进制字符串
# 发送文件
file_dict = {
'f1': open('xxxx.log', 'rb')
}
requests.request(
method='POST',
url='http://127.0.0.1:8000/test/',
files=file_dict
)
9、auth 认证(headers中加入加密的用户名和密码)
内部:
用户名和密码,用户和密码加密,放在请求头中传给后台。
- "用户:密码"
- base64("用户:密码")
- "Basic base64("用户|密码")"
- 请求头:
Authorization: "basic base64("用户|密码")"
from requests.auth import HTTPBasicAuth, HTTPDigestAuthret = requests.get('https://api.github.com/user', auth=HTTPBasicAuth('wupeiqi', 'sdfasdfasdf'))
print(ret.text)
10、timeout 请求和响应的超时时间
# ret = requests.get('http://google.com/', timeout=1)
# print(ret)
# ret = requests.get('http://google.com/', timeout=(5, 1))
# print(ret)
11、allow_redirects 是否允许重定向
ret = requests.get('http://127.0.0.1:8000/test/', allow_redirects=False)
print(ret.text)
12、proxies 代理
# 无验证
proxie_dict = {
"http": "61.172.249.96:80",
"https": "http://61.185.219.126:3128",
}
ret = requests.get("https://www.proxy360.cn/Proxy", proxies=proxie_dict)
# 验证代理
from requests.auth import HTTPProxyAuth
proxyDict = {
'http': '77.75.105.165',
'https': '77.75.106.165'
}
auth = HTTPProxyAuth('用户名', '密码')
r = requests.get("http://www.google.com",data={'xxx':'ffff'} proxies=proxyDict, auth=auth)
print(r.text)
13、verify 是否忽略证书
14、cert 证书文件
- 百度、腾讯 => 不用携带证书(系统帮你做了)
- 自定义证书
requests.get('http://127.0.0.1:8000/test/', cert="xxxx/xxx/xxx.pem")
requests.get('http://127.0.0.1:8000/test/', cert=("xxxx/xxx/xxx.pem","xxx.xxx.xx.key"))
15、stream 大文件下载
from contextlib import closing
with closing(requests.get('http://httpbin.org/get', stream=True)) as r1:
# 在此处理响应。
for i in r1.iter_content():
print(i)
16、session: 用于保存客户端历史访问信息
ession
session = requests.Session()
session.get()
session.post()