-1

I am making get/Post request on a URL and in response getting an HTML page. I only want a response header, no response body. already used HEAD method but it is not working in all kind of situations. By getting complete HTML page in response, bandwidth is increasing. and also need a solution so it will work in both https and HTTP request.

For Example

import urllib2
urllib2.urlopen('http://www.google.com')

if I am sending a request on this URL using urllib2 or request. I am getting both response body and header from the server. this request is taking 14.08 kb in bytes. If I break this, the response header is taking 775 bytes and response body is taking 13.32kb. so I need only response header and will save 13.32 kb

2 Answers2

1

Is this what you are looking for:

import urllib2
l = urllib2.urlopen('http://www.google.com')

print(l.headers)
#Date: Thu, 11 Oct 2018 09:07:20 GMT
#Expires: -1
#...

EDIT

This seems to do what you are looking for:

import requests
a = requests.head('https://www.google.com')

a.headers
#{'X-XSS-Protection': '1; mode=block', 'Content-Encoding':...

a.text
#u''
zipa
  • 27,316
  • 6
  • 40
  • 58
  • No, In this Case, if I run this I am getting both response body and header from the server. this request is taking 14.08 kb in bytes. If I break this, the response header is taking 775 bytes and response body is taking 13.32kb. so I need only response header is can save 13.32 kb. – Nirbhay Jain Oct 11 '18 at 10:17
  • @NirbhayJain Please see the edit, hopefully that's the one you are looking for – zipa Oct 11 '18 at 11:43
  • can we send head request with HTTPS method? – Nirbhay Jain Oct 11 '18 at 12:52
1

What you want to do is a so called HEAD request. See this question on how to do it.

Robsdedude
  • 1,292
  • 16
  • 25
  • But Head request is dependent on the server and it is not working in every situation and also in case of https, I cant use head. – Nirbhay Jain Oct 11 '18 at 10:30
  • If you don't want to make a `HEAD` request then there is no way to tell the server not so send the body. The body just belongs to the HTTP response. You can discard the body afterwards as @zipa answered. But that's about it you can do. – Robsdedude Oct 11 '18 at 10:33
  • I think first it reads response header then it will read response body so what if I close the connection after response header reads or limit the downloading bandwidth – Nirbhay Jain Oct 11 '18 at 10:36
  • When sending the request the response will come in and be stored in some buffer. Whether you read it or not. The TCP/IP stack will just "do it's thing". You could write low level stuff using sockets to end the connection after you've received the header. But that sound like a dirty hack that is very error prone. – Robsdedude Oct 11 '18 at 10:39
  • Curl is a (linux) command line program that does http(s) requests. `curl -I ...` just sends a `HEAD` request and prints the returned header. The question I linked above tells you how to make such a `HEAD` request with python libs. Just give it a try. – Robsdedude Oct 11 '18 at 10:48