request

来源:互联网 发布:ubuntu哪个版本最稳定 编辑:程序博客网 时间:2024/06/10 16:43

发送请求

发送请求,以post为例
1.param形式传参:

import requestsurl = 'http://www.wozuishuai.com'param= {    "params1":"value1"}requests.post(url=url, params=param)

2.body形式传参:

url = 'http://www.wozuishuai.com'body = {    "params1":"value1"}requests.post(url=url, json=body)

3.x-www-form-urlencoded传参格式:

import requestsurl = 'http://www.wozuishuai.com'data = {    "params1":"value1"}requests.post(url=url, data=data)

4.multipart/form-data传参格式:

import requestsurl = 'http://www.wozuishuai.com'files = {    "params1":"value1",    "files": ("1.png", open("这里是路径","rb"),"image/png")}r = requests.post(url=url, files=files)

5.定制header

import requestsheaders={    'Connection': 'keep-alive',     'Content-Type': 'multipart/form-data; '}url = 'http://www.wozuishuai.com'r = requests.get(url=url,headers=headers)

聪明的人已经看出来了,想传什么参数只要对应一下参数名就OK了。其实requests里面有介绍的:

: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) json data 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.
原创粉丝点击