Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to stay logged in to a website with Python while running a program and requesting data?
    primarykey
    data
    text
    <p>I found a way to stay logged in to a website with Python. The problem is that every once in a while, I get disconnected and logged out. I'm guessing that the session is timing out but I don't know how to fix it.</p> <p>I used the Live HTTP Headers add-on for Firefox and copied headers from my login request into my program.</p> <pre><code>import urllib import urllib2 import cookielib data = urllib.urlencode({"inUserName":"MY EMAIL", "inUserPass":"MY PASSWORD"}) jar = cookielib.FileCookieJar("cookies") opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) opener.addheaders.append(('User-agent', 'Mozilla/5.0')) opener.addheaders.append(('Referer', 'http://www.locationary.com/')) opener.addheaders.append(('Cookie','site_version=REGULAR; __utma=47547066.601656247.1344371114.1344602507.1344606239.16; __utmz=47547066.1344371114.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; jforumUserId=1; locaCountry=1227; locaState=null; locaCity=Atlanta; PSESSIONID=533e2fb9fda008d5d16bfbdc9b9a6afed0e5ac54; Locacookie=enable; sortOrder=1; JSESSIONID=DE58AC8BC78D1DF20BF338E195336E58; __utmc=47547066; __utmb=47547066.6.10.1344606239')) request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data) response = opener.open(request) page = opener.open(url).read() soup = BeautifulSoup(page) </code></pre> <p>I used <code>cookielib</code> and <code>urrlib</code>/<code>urllib2</code>. The cookie that really makes it work is the really long one, But I don't really know what it all means and I just copied it in from the add-on. If my connection goes out, I go to my browser and log in again and get a new cookie from the add-on. Like I said before, I'm guessing it has to do with the session or the sessionid or something, but I don't know how I can make it so that I am always logged in.</p> <p>Thanks.</p> <h3>EDIT</h3> <p>Can someone tell me why this is a bad question or am I just stupid? -1 for what?</p> <h3>EDIT 2</h3> <p>Okay! If I don't <strong>stay</strong> logged in, then is there a way to keep my connection/cookie from not working?</p> <h3>EDIT 3</h3> <p>I don't know how to <strong>get a new cookie</strong> other than going to the Firefox add-on myself...haha</p> <h3>EDIT 4</h3> <p>Okay. I made a new test program:</p> <pre><code>import urllib import urllib2 import cookielib import re url = 'http://www.locationary.com/home/index2.jsp' data = urllib.urlencode({"inUserName":"email", "inUserPass":"password"}) jar = cookielib.FileCookieJar("cookies") opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(jar)) opener.addheaders.append(('User-agent', 'Mozilla/5.0 (Windows NT 6.1; rv:13.0) Gecko/20100101 Firefox/13.0.1')) opener.addheaders.append(('Referer', 'http://www.locationary.com/')) opener.addheaders.append(('Cookie','site_version=REGULAR; __utma=47547066.601656247.1344371114.1344612897.1344615635.18; __utmz=47547066.1344371114.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); nickname=jacob501; jforumUserId=1; locaCountry=1227; locaState=null; locaCity=Atlanta; PSESSIONID=533e2fb9fda008d5d16bfbdc9b9a6afed0e5ac54; Locacookie=enable; sortOrder=1; JSESSIONID=781FD0C497FB596954BB78B1323215F6; __utmc=47547066; __utmb=47547066.9.10.1344615635')) request = urllib2.Request("https://www.locationary.com/index.jsp?ACTION_TOKEN=tile_loginBar_jsp$JspView$LoginAction", data) response = opener.open(request) page = opener.open(url).read() print re.findall(r'&lt;title&gt;(.*)&lt;/title&gt;', page) h = response.info().headers print h </code></pre> <p>Output:</p> <pre><code>['Home Page'] ['Server: nginx/1.0.8\r\n', 'Date: Fri, 10 Aug 2012 16:50:47 GMT\r\n', 'Content-Type: text/html;charset=UTF-8\r\n', 'Transfer-Encoding: chunked\r\n', 'Connection: close\r\n', 'P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"\r\n'] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload