2

Possible Duplicate:
How do you send a HEAD HTTP request in Python?

I am using Python's urllib and urllib2 to do an automated login. I am also using HTTPCookieProcessor to automate the handling of the cookies. The code is somewhat like this:

o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
# assuming the site expects 'user' and 'pass' as query params
p = urllib.urlencode( { 'username': 'me', 'password': 'mypass' } )
# perform login with params
f = o.open( 'http://www.mysite.com/login/',  p )
data = f.read()
f.close()
# second request
t = o.open( 'http://www.mysite.com/protected/area/' )
data = t.read()
t.close()

Now, the point is that I don't want to waste bandwidth in downloading the contents of http://www.mysite.com/login/, since all I want to do is receive the cookies (which are there in the Headers). Also, the site redirects me to http://www.mysite.com/userprofile when I first login (that is, the f.geturl() = http://www.mysite.com/userprofile).

So is there any way that I can avoid fetching the content in the first request?

P.S. Please don't ask me why am I avoiding the small network usage of transferring the content. Although the content is small, I still don't want to download it.

Community
  • 1
  • 1
siddhant3s
  • 410
  • 3
  • 10

1 Answers1

1

Just send a HEAD request instad of a GET request. You can use Python's httplib to do that.

Something like this:

   import httplib, urllib
   creds = urllib.urlencode({ 'username': 'me', 'password': 'mypass' });
   connection = httplib.HTTPConnection("www.mysite.com")
   connection.request("HEAD", "/login/", creds)
   response = connection.getresponse()
   print response.getheaders()
Alex
  • 64,178
  • 48
  • 151
  • 180
  • But then, I would lose the facility of HTTPCookieProcessor, right? – siddhant3s Nov 20 '10 at 06:27
  • @siddhan3s Rather than answer something that's already been answered, see this answer http://stackoverflow.com/questions/107405/how-do-you-send-a-head-http-request-in-python/2070916#2070916 You can use urllib2 to make HEAD requests as well. – Alex Nov 20 '10 at 06:29