HANDLING COOKIES

In Perl the CGI::Cookie module will create a cookie HTTP header for you by supplying the cookie data, domain, and expiry date to a new cookie object. For example

my $query = new CGI;
my $cookietext = $query->cookie(-name => 'sample',
                               -value => { login =>
                                           $login,
                                           other =>
                                          'Other'
                               -path => '/',
                               -domain =>
                                   'mcwords.mchome.com',
                               -expires => '+1y');
print "Set-Cookie: $cookietext\n";

In Python there is a third party module called Cookie which will build a new cookie for you. For example, we could rewrite the above using the Cookie module like this:

 import Cookie cookie = Cookie.SmartCookie() cookie['sample'] = 'login=%s; other=Other' % (login) cookie['sample']['path'] = '/' cookie['sample']['domain'] = 'mcwords.mchome.com' cookie['sample']['expires'] ...

Get Perl To Python Migration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.