Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 3 and Version 4 of ConvertCookies


Ignore:
Timestamp:
06/24/07 17:58:53 (12 years ago)
Author:
Chris Arndt
Comment:

migration notice

Legend:

Unmodified
Added
Removed
Modified
  • ConvertCookies

    v3 v4  
    1 = Converting Between Cookie Types = 
    2  
    3 There are two largely incompatible cookie types in the Python standard library.  The Cookie library is used by !CherryPy.  Urllib2 (which is handy for accessing other websites as a client) uses cookielib.  The code below is at least a start at converting between the two.  It has only been lightly tested, but is hopefully better than starting from scratch.  If you find a bug, please fix it. :-) 
    4  
    5  
    6 cookie_converter.py 
    71{{{ 
    8 #!python 
    9 import cookielib 
    10 import Cookie 
    11 import time 
    12  
    13 class FakeRequest(object): 
    14     """A request object that pretends it is a urlib2.Request. 
    15      
    16     Not a full implementation, but enough to get by. 
    17     """ 
    18      
    19     def __init__(self, cherrypy_request): 
    20         self.req = cherrypy_request 
    21          
    22     def get_full_url(self): 
    23         return self.req.browser_url 
    24          
    25     def get_host(self): 
    26         return self.req.headers['Host'] 
    27          
    28     def get_type(self): 
    29         return self.req.scheme 
    30          
    31     def is_unverifiable(self): 
    32         # Assuming everything has been verified 
    33         return False 
    34          
    35     def get_origin_req_host(self): 
    36         # Again, this is more of a client function, so punting 
    37         return self.req.headers['Host'] 
    38      
    39  
    40 def create_cookiejar(cherrypy_request): 
    41     "Returns a cookielib.CookieJar based on cookies found in cherrypy.request" 
    42      
    43     req = cherrypy_request 
    44     cookiejar = cookielib.CookieJar() 
    45     # fake request to make cookielib happy 
    46     fake_req = FakeRequest(req) 
    47     for cookie_name, morsel in req.simple_cookie.items(): 
    48         std_attr = {} 
    49         # copy attributes that have values 
    50         for attr in morsel.keys(): 
    51             if morsel[attr]: 
    52                 std_attr[attr]=morsel[attr]  
    53         tup = (cookie_name, morsel.value, std_attr, {}) 
    54         new_cookie = cookiejar._cookie_from_cookie_tuple(tup, fake_req) 
    55          
    56         cookiejar.set_cookie(new_cookie) 
    57          
    58     return cookiejar 
    59      
    60 def create_simple_cookie(cookiejar): 
    61     "Returns a Cookie.SimpleCookie based on cookielib.CookieJar." 
    62     sc = Cookie.SimpleCookie() 
    63     attrs = ('expires', 'path', 'comment', 'domain', 'secure', 'version') 
    64     # Doesn't look like Cookie.SimpleCookie allows for nonstandard attributes, so  
    65     # we only deal with the standard ones. 
    66     for path_dict in cookiejar._cookies.values(): #iterate through paths 
    67         for cookie_dict in path_dict.values():     
    68             for name, cookie in cookie_dict.items(): 
    69                 sc[name] = cookie.value 
    70                 for attr in attrs: 
    71                     if getattr(cookie, attr): 
    72                         if attr == 'expires': 
    73                             # Cookies thinks an int expires x seconds in future, 
    74                             # cookielib thinks it is x seconds from epoch, 
    75                             # so doing the conversion to string for Cookies 
    76                             fmt = '%a, %d %b %Y %H:%M:%S GMT' 
    77                             sc[name]['expires'] = time.strftime(fmt,  
    78                                                     time.gmtime(cookie.expires)) 
    79                         else: 
    80                             sc[name][attr] = getattr(cookie, attr) 
    81     return sc 
     2#!rst 
     3.. note:: This page has been migrated to http://docs.turbogears.org/1.0/ConvertCookies. 
    824}}}