1.预配置
![]()
import requests
ss = requests.Session()
ss.headers.update({'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0'})
# C:\Program Files\Anaconda2\lib\site-packages\urllib3\connectionpool.py:858: InsecureRequestWarning:
# Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: ht
# tps://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
ss.verify = False
from urllib3.exceptions import InsecureRequestWarning
from warnings import filterwarnings
filterwarnings('ignore', category = InsecureRequestWarning)
# http://docs.python-requests.org/zh_CN/latest/api.html
import logging
try:
from http.client import HTTPConnection
except ImportError:
from httplib import HTTPConnection
HTTPConnection.debuglevel = 0 #关闭 0 开启 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
def s_hooks(r, *args, **kwargs):
print('#'*20)
print(r.url, r.request.method, r.status_code, r.reason)
print('request Content-Type: {} body: {}'.format(r.request.headers.get('Content-Type'), r.request.body))
print('response Content-Type: {} body length: {}'.format(r.headers.get('Content-Type'), len(r.content)))
if 'json' in r.headers.get('Content-Type', '').lower():
print(r.content)
print('#'*20)
ss.hooks = dict(response=s_hooks)
def httpbin(postfix=''):
return 'http://httpbin.org/' + postfix
# r = ss.post(httpbin('post'),json=dict([('a',1),('b',2)]) )
View Code