Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
检查会话cookie是否仍处于活动状态(Python)_Python_Session Cookies_Urllib_Cookiejar_Cookielib - Fatal编程技术网

检查会话cookie是否仍处于活动状态(Python)

检查会话cookie是否仍处于活动状态(Python),python,session-cookies,urllib,cookiejar,cookielib,Python,Session Cookies,Urllib,Cookiejar,Cookielib,请查看以下python代码片段: import cookielib, urllib, urllib2 def login(username, password): cookie_jar = cookielib.LWPCookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar)) login = urllib.urlencode({'username': username

请查看以下python代码片段:

import cookielib, urllib, urllib2

def login(username, password):
    cookie_jar = cookielib.LWPCookieJar()
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    login = urllib.urlencode({'username': username, 'password': password})
    try:
        login_data = opener.open("http://www.example.com/login.php", login).read()
    except IOError:
        return 'Network Error'

    # on successful login the I'm storing the 'SESSION COOKIE'
    # that the site sends on a local file called cookie.txt

    cookie_jar.save('./cookie.txt', True, True)
    return login_data

# this method is called after quite sometime
# from calling the above method "login" 

def re_accessing_the _site():
    cookie_jar = cookielib.LWPCookieJar()

    # Here I'm re-loading the saved cookie
    # from the file to the cookie jar

    cookie_jar.revert('./cookie.txt', True, True)

    # there's only 1 cookie in the cookie jar
    for Cookie in cookie_jar:
        print 'Expires : ', Cookie.expires  ## prints None
        print 'Discard : ', Cookie.discard  ## prints True , means that the cookie is a
                                            ## session cookie
        print 'Is Expired : ', Cookie.is_expired()  ## prints False

    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
    try:
        data = opener.open("http://www.example.com/send.php")

        # Sometimes the opening fails as the cookie has expired
        # & sometimes it doesn't. Here I want a way to determine
        # whether the (session) cookie is still alive

    except IOError:
        return False
    return True
首先,我调用方法login&将检索到的cookie(这是一个会话cookie)保存到名为cookie.txt的本地文件中。 接下来过了一段时间(15-20分钟),我将调用另一种方法重新访问网站。这一次,我还将以前保存的cookie重新加载到cookiejar中。
有时它工作正常,但有时它会阻止我访问(,因为会话cookie已过期)。因此,我所需要的只是一种检查cookie在调用过程中是否仍处于活动状态的方法…

身份验证的时间戳可能存在于cookie中的数据中,甚至可能存在于服务器端。在这种情况下,可能无法通过询问Cookie对象来知道。如果是这种情况,那么您只需自己缓存用户名/密码,捕获错误,再次登录并重试。只是出于兴趣,你能粘贴Cookie吗?_dict___;在这里,因为它可能在某个地方有更有用的信息…身份验证的时间戳可能在Cookie中的数据中,或者甚至保存在服务器端。在这种情况下,可能无法通过询问Cookie对象来知道。如果是这种情况,那么您只需自己缓存用户名/密码,捕获错误,再次登录并重试。只是出于兴趣,你能把饼干粘贴到这里吗?_udict__;在这里,因为它可能在某处有更有用的信息。。。