我试图在连续运行程序时保持登录到网站。会话最终会超时。
这是我的测试程序:
import urllib
import urllib2
import cookielib
import re
url = 'https://www.locationary.com/'
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; JSESSIONID=781FD0C497FB596954BB78B1323215F6'))
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'<title>(.*)</title>', page)
h = response.info().headers
print h
输出是:
['Home Page']
['Server: nginx/1.0.8\r\n', 'Date: Fri, 10 Aug 2012 17:07:47 GMT\r\n', 'Content-Type: text/html;charset=UTF-8\r\n', 'Transfer-Encoding: chunked\r\n', 'Connection: close\r\n', 'Set-Cookie: PSESSIONID=533e2fb9fda008d5d16bfbdc9b9a6afed0e5ac54; Path=/\r\n', 'P3P: CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"\r\n', 'Set-Cookie: PSESSIONID=533e2fb9fda008d5d16bfbdc9b9a6afed0e5ac54; Path=/\r\n']
我进行了实验......如果我取出 cookie 的 JSESSIONID 部分,我的输出将是网站的主页,就好像我没有登录一样......所以它会说“Locationary.com”而不是“主页。” 当我将 JSESSIONID 放入时,一切正常,但服务器还设置了一个名为 PSESSIONID 的 cookie……这一切意味着什么……我怎样才能使它不会超时?我必须每隔半小时找到另一个 JSESSIONID 吗?