Advertisement
rfmonk

extract_cookie_info.py

Jul 3rd, 2014
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.01 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # thanks to py netw prog cookbk
  3.  
  4. import cookielib
  5. import urllib
  6. import urllib2
  7.  
  8. ID_USERNAME = 'id_username'
  9. ID_PASSWORD = 'id_password'
  10. USERNAME = 'you@email.com'
  11. PASSWORD = 'mypassword'
  12. LOGIN_URL = 'https://bitbucket.org/account/signin/?next=/'
  13. NORMAL_URL = 'https://bitbucket.org/'
  14.  
  15. def extract_cookie_info():
  16.     """ Fake login to site with cookie """
  17.     # Setup the cookie jar
  18.     cj = cookielib.CookieJar()
  19.     login_data = urllib.urlencode({ID_USERNAME : USERNAME, \
  20.         ID_PASSWORD : PASSWORD})
  21.     # Create url opener
  22.     opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  23.     resp = opener.open(LOGIN_URL, login_data)
  24.  
  25.     # Send login info
  26.     for cookie in cj:
  27.         print "----First time cookie: %s --> %s" %(cookie.name, cookie.value)
  28.         print "Headers: %s" %resp.headers
  29.  
  30.     # Now access without any login info
  31.     resp = opener.open(NORMAL_URL)
  32.     for cookie in cj:
  33.         print "++++Second time cookie: %s --> %s" %(cookie.name, cookie.value)
  34.         print "Headers: %s" %resp.headers
  35.  
  36. if __name__ == '__main__':
  37.     extract_cookie_info()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement