#!/usr/bin/python3
###################################################################################
# Set cookies
# Using Python http.cookies module
# All cookies must be written before html output
###################################################################################
#import Cookie                      #use for Python 2x
from http import cookies            #use for Python 3x
import datetime
import cgitb
cgitb.enable()

print('Content-Type: text/html')    # required header

c = cookies.SimpleCookie()          #create an object that helps creates cookies

### Set a cookie that is temporary ######################################
c['name1'] = 'Sam1'                                                     # session based only


### A cookie that expires in 5 minutes ##################################
c['name2'] = 'Sam2'
c['name2']['max-age'] = 300                                             # 300 seconds


### A cookie that expires at a specific time ############################
c['name3'] = 'Sam3'
c['name3']['expires'] = 'Sun, 31 Dec 2023 23:59:59 GMT'                 # Day, DD Mth YYYY HH:MM:SS GMT


### A cookie that expires at a specific time (more dynamic) #############
c['name4'] = 'Sam4_Sultan'
expires = datetime.datetime(2023, 12, 31, 23, 59, 59)
c['name4']['expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')   # Day, DD Mon YYYY HH:MM:SS GMT


### A cookie with additional cookie options #############################
c['name5'] = 'Sam5'
c['name5']['path']     = '/'                                            # available to this path
c['name5']['domain']   = 'samsultan.com'                                # available to this domain 
c['name5']['httponly'] = True                                                                                           
#c['name5']['secure']  = True


print(c)                                # Create/print the Set-Cookie HTTP header each on separate line
#print(c.output())                      # the same as above 

print('\n')                             # required end of HTTP headers

#---------------------------------------------------------------------------------------

print("<h1>Setting Cookies - Using http.cookies Module</h1>")

print("<p>This page set 5 cookies: <b>name1, name2, name3, name4, name5</b></p>")
print("<br>")

print(c['name1'], '<br>')
print(c['name2'], '<br>')
print(c['name3'], '<br>')
print(c['name4'], '<br>')
print(c['name5'], '<br>')

print("<br><b>All Cookie Headers Together...</b><br>")
print("Although you see them all on one line, there is a '\n' character in between each<br><br>")
print(c)



#=== link to see the python code =================================================
import os, sys
sys.path.insert(0,'/home/s/sultans/web/python/demo')
import zCode                          #import func to display the Python code
filename = os.path.abspath(__file__)  #get absolute file name 
zCode.display(filename)               #call it
#=================================================================================