Python - using cookies successfully
When I try to use facebook cookies to open a registered facebook account ...
import urllib2, urllib, cookielib
jar = cookielib.CookieJar()
cookie = urllib2.HTTPCookieProcessor(jar)
opener = urllib2.build_opener(cookie)
data = urllib.urlencode({'email':'user@email.com','pass':'swagpassword','login':'Log+In'})
req = urllib2.Request('http://www.facebook.com/login.php')
response = opener.open(req, data)
cookie_header = response.headers.get("Set-Cookie")
response = opener.open(req, data) #I open it twice on purpose
if "Logout" in response.read():
print("Logged In")
jar = cookielib.CookieJar() #new instance
cookie = urllib2.HTTPCookieProcessor(jar) #new instance
opener = urllib2.build_opener(cookie) #new instance
cookie_request = urllib2.Request('http://www.facebook.com/login.php')
cookie_request.add_header("cookie", cookie_header)
cookie_POST = opener.open(cookie_request)
cookie_POST = opener.open(cookie_request)
if "Logout" in cookie_POST.read():
print("Logged In")
It will print "Logged In" the first time successfully, but when I try to use the cookie, I am not logged in. How can I fix this? (Without using other loaded modules)
+3
source to share
1 answer
Just reuse old instances.
....
if "Logout" in response.read():
print("Logged In")
#jar = cookielib.CookieJar() #new instance
#cookie = urllib2.HTTPCookieProcessor(jar) #new instance
#opener = urllib2.build_opener(cookie) #new instance
cookie_request = urllib2.Request('http://www.facebook.com/login.php')
#cookie_request.add_header("cookie", cookie_header)
cookie_POST = opener.open(cookie_request)
...
+2
source to share