python-requests: fetching the head of the response content without consuming it all

前端 未结 2 1975
南旧
南旧 2020-12-30 16:27

Using python-requests and python-magic, I would like to test the mime-type of a web resource without fetching all its content (especially if this resource happens to be eg.

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 17:11

    if 'content-type' suffices, you can issue an HTTP 'Head' request instead of 'Get', to just receive the HTTP headers.

    import requests
    
    url = 'http://www.december.com/html/demo/hello.html'
    response = requests.head(url)
    print response.headers['content-type']
    

提交回复
热议问题