Python - How do I save/load specific cookies? -


i have following python code supposed log website using .roblosecurity cookie. includes except ioerror: function if .roblosecurity cookie doesn't log in, use username/password log in , save cookies gets that.

import urllib2 import urllib import cookielib  try:     cookielib.lwpcookiejar().load("cookies.txt") #trying load cookie file  except ioerror: #in case cookies.txt fails log in. don't know if ioerror correct error specification expired cookie     print "loading stored cookies failed, manually logging in..."     cj = cookielib.lwpcookiejar()     opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj))     opener.addheaders = [('user-agent', 'mozilla/5.0')]     urllib2.install_opener(opener)     authentication_url = 'https://www.roblox.com/newlogin'     payload = {         'username' : 'usernamehere',         'password' : 'passwordhere',         '' : 'log in',         }     data = urllib.urlencode(payload)     req = urllib2.request(authentication_url, data)     resp = urllib2.urlopen(req)     cj.save("cookies.txt") #saving cookie file  tc = urllib2.urlopen("http://www.roblox.com/my/money.aspx").read() #the hidden page checksession = re.findall('my transactions',tc) #just checking text found on hidden page print checksession 

i think cookielib.lwpcookiejar().load("cookies.txt") not working because loading other cookies other .roblosecurity (which know logs in if use that). how either load .roblosecurity cookie or save .roblosecurity (so other cookies don't interfere .roblosecurity logging in)?

also, i'm not sure if except ioerror: function correctly because know that works if change cookielib.lwpcookiejar().load("cookies.txt") cookielib.mozillacookiejar().load("cookies.txt")

lastly, how can change .roblosecurity's expiration date 2050-12-31 24:00:00z

so here final code:

import urllib #idk if necessary, i'm not gonna bother checking. time used urllib.urlencode, idk if urllib2 can import urllib2 import cookiejar try:     cj = cookielib.mozillacookiejar("./cookies.txt")     cj.load()     opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj))     tc = opener.open("http://www.roblox.com/my/money.aspx").read()  #    print "loading stored cookies succeeded, automatically logging in..."  #    checksession = re.findall('my transactions',tc)  #    if len(checksession) > 0: #        print "login success!"  except ioerror:     print "loading stored cookies failed, manually logging in..."     opener = urllib2.build_opener(urllib2.httpcookieprocessor(cj))     opener.addheaders = [('user-agent', 'mozilla/5.0')]     urllib2.install_opener(opener)     authentication_url = 'https://www.roblox.com/newlogin'     payload = {         'username' : 'usernamehere',         'password' : 'passwordhere',         '' : 'log in', #in hindsight idk if necessary, don't feel checking         }     data = urllib.urlencode(payload)     req = urllib2.request(authentication_url, data)     resp = urllib2.urlopen(req)     cj.save("./cookies.txt") #./ make sure in right directory      tc = opener.open("http://www.roblox.com/my/money.aspx").read() #only accessible if logged in #    checksession = re.findall('my transactions',tc) #the rest check if log in success looking specific phrase in page  #    if len(checksession) > 0: #        print "login success!" 

Comments

Popular posts from this blog

node.js - How to mock a third-party api calls in the backend -

java - Could not locate OpenAL library -

Non Unique Username with ASP.Net Identity 2.0 -