print set-cookie hdr cookies['user''brianprint(cookiesgreeting 'his name shall be %scookies['user'elsegreeting 'welcome back%susercook value print('content-typetext/html\ 'print(greetingplus blank line now and the actual html assuming you are running this local web server from example - you can invoke this script with url such as your browser' address fieldor submit it interactively with the module url lib requestthe first time you visit the scriptthe script sets the cookie within its reply' headersand you'll see reply page with this messagehis name shall be set-cookieuser=brian thereafterrevisiting the script' url in the same browser session (use your browser' reload buttonproduces reply page with this messagewelcome backbrian this occurs because the client is sending the previously stored cookie value back to the scriptat least until you kill and restart your web browser--the default expiration of cookie is the end of browsing session in realistic programthis sort of structure might be used by the login page of web applicationa user would need to enter his name only once per browser session handling cookies with the urllib request module as mentioned earlierthe urllib request module provides an interface for reading the reply from urlbut it uses the http cookiejar module to also support storing and sending cookies on the client howeverit does not support cookies "out of the box for examplehere it is in action testing the last section' cookie-savvy script--cookies are not echoed back to the server when script is revisitedfrom urllib request import urlopen reply urlopen(print(replyb'his name shall be set-cookieuser=brian\nreply urlopen(print(replyb'his name shall be set-cookieuser=brian\nto support cookies with this module properlywe simply need to enable the cookiehandler classthe same is true for other optional extensions in this module againcontacting the prior section' scriptimport urllib request as urllib opener urllib build_opener(urllib httpcookieprocessor()urllib install_opener(opener server-side scripting