Requests: HTTP library designed for human

Keywords: Python JSON Windows pip

brief introduction

Article environment

Windows 10
Python 3.6.x
Requests 2.x

install

pip install requests  -i https://mirrors.aliyun.com/pypi/simple/

Tips and examples

cookie

  • Add cookie
requests.utils.add_dict_to_cookiejar(cj, cookie_dict)
  • If sn.headers['Cookie '] has a value, then sn.cookies no longer work

Close https certificate warning

import requests
from urllib3.exceptions import InsecureRequestWarning
# Close https certificate warning
requests.urllib3.disable_warnings(InsecureRequestWarning)

Advanced Usage

Default timeout

get url 25%

The percentage sign (%) in the url when Requests are get is forced to be encoded as 25%, which can be bypassed as follows:

import requests
sn = requests.Session()
url = 'http://xxx.net/xxx.aspx?Param=ASP.brief_result_aspx%23/%u5E74
req = requests.Request('GET', url)
prepped = sn.prepare_request(req)
prepped.url = prepped.url.replace('%25', '%')
r = sn.send(prepped)

Or reassemble after splitting:

import requests
from urllib import parse
url = 'http://xxx.net/xxx.aspx?' + parse.urlencode(dict(parse.parse_qsl(parse.urlparse(url).query)))
r = sn.get(url)

POST request

[code]

import requests
import json

r = requests.post('http://www.baidu.com', files={'key':'val'})
print('***test 1: %s' % r.request.body)
print('------------------------')
print(r.request.body.decode('utf8'))
print('\n')

line = json.dumps({'k1':'v1', 'k2':'v2'})
r = requests.post('http://www.baidu.com', files={'json':(None, line)})
print('***test 2: %s' % r.request.body)
print('------------------------')
print(r.request.body.decode('utf8'))

[output]

***test 1: b'--5b57f36c03ca462f93dfbd8dfc97e2d1\r\nContent-Disposition: form-data; name="key"; filename="key"\r\n\r\nval\r\n--5b57f36c03ca462f93dfbd8dfc97e2d1--\r\n'
------------------------
--5b57f36c03ca462f93dfbd8dfc97e2d1
Content-Disposition: form-data; name="key"; filename="key"

val
--5b57f36c03ca462f93dfbd8dfc97e2d1--


***test 2: b'--cf66a355e1f6441fa5a3079e1fafc43a\r\nContent-Disposition: form-data; name="json"\r\n\r\n{"k1": "v1", "k2": "v2"}\r\n--cf66a355e1f6441fa5a3079e1fafc43a--\r\n'
------------------------
--cf66a355e1f6441fa5a3079e1fafc43a
Content-Disposition: form-data; name="json"

{"k1": "v1", "k2": "v2"}
--cf66a355e1f6441fa5a3079e1fafc43a--

This article is from walker snapshot

Posted by Chrysanthus on Wed, 23 Oct 2019 14:05:41 -0700