How do I clear cache with Python Requests?

后端 未结 4 1093
北荒
北荒 2020-12-06 09:35

Does the requests package of Python cache data by default?

For example,

import requests
resp = requests.get(\'https://some website\')
         


        
4条回答
  •  遥遥无期
    2020-12-06 10:06

    Late answer, but python requests doesn't cache requests, you should use the headers Cache-Control and Pragma instead, i.e.:

    import requests
    headers = {
        ...
        "Cache-Control": "no-cache",
        "Pragma": "no-cache"
    }
    x = requests.get("site.tld", headers=headers)
    ...
    

    HTTP/Headers

    • Cache-Control
      The Cache-Control general-header field is used to specify directives for caching mechanisms in both requests and responses. Caching directives are unidirectional, meaning that a given directive in a request is not implying that the same directive is to be given in the response.

    • Pragma
      Implementation-specific header that may have various effects anywhere along the request-response chain. Used for backwards compatibility with HTTP/1.0 caches where the Cache-Control header is not yet present.


    Directive

    • no-cache
      Forces caches to submit the request to the origin server for validation before releasing a cached copy.


    Note on Pragma:

    Pragma is not specified for HTTP responses and is therefore not a reliable replacement for the general HTTP/1.1 Cache-Control header, although it does behave the same as Cache-Control: no-cache, if the Cache-Control header field is omitted in a request. Use Pragma only for backwards compatibility with HTTP/1.0 clients.

提交回复
热议问题