• python之requests 乱七八糟


    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 FilesAnaconda2libsite-packagesurllib3connectionpool.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

    2.Content-Type

    Sending JSON in Requests

    import requests
    from pprint import pprint
    ss = requests.Session()
    ss.headers['Content-Type'] = 'application/myjson'
    r = ss.post('https://httpbin.org/post', json={'my': 'json'}, headers={'Content-Type':'somejson'})
    pprint(r.json())
    View Code

    优先级由高到低: 传参 headers={'Content-Type':'somejson'}  >>> ss.headers >>> 传参 json= 自动生成的 u'Content-Type': u'application/json'

    3. Using Retries in requests

    Retries in Requests

    (1)简单粗暴

    s.mount('https://', HTTPAdapter(max_retries=5))

    (2)更细粒度

    from requests.packages.urllib3.util import Retry
    from requests.adapters import HTTPAdapter
    from requests import Session
    
    s = Session()
    s.mount('https://pypi.python.org/', HTTPAdapter(
        max_retries=Retry(total=5, status_forcelist=[500, 503])
        )
    )
    r = s.get('https://pypi.python.org/simple/requests/')
    View Code
  • 相关阅读:
    python之squid实现免费 IP代理 (windows win7 单机 本机 本地 正向代理 区分 HTTPS)
    python之PIL 二值图像处理和保存
    python之GIL release (I/O open(file) socket time.sleep)
    python之多线程 threading.Lock() 和 threading.RLock()
    python之GIL官方文档 global interpreter lock 全局解释器锁
    python多线程之t.setDaemon(True) 和 t.join()
    python之工作目录和文件引用
    python 代理
    原 浅谈移动端开发--物理像素和逻辑像素
    解决移动端页面在苹果端滑不到底部的问题
  • 原文地址:https://www.cnblogs.com/my8100/p/7347425.html
Copyright © 2020-2023  润新知