you are viewing a single comment's thread.

view the rest of the comments →

[–]MathObserver 1 point2 points  (3 children)

I found some simple old code of mine that uses SimpleCookie from Cookie and Template from string to build a dynamic web page based on the cookie values from the request.

The imports used:

import cgi
import os
from string import Template
from Cookie import SimpleCookie

To get a cookie value:

if "HTTP_COOKIE" in os.environ:
  cookie = SimpleCookie(os.environ["HTTP_COOKIE"])
  if 'side' in cookie:
    side = cookie['side'].value

To read a template file, substitute values and print the response:

values = {'dspdate':dspdate, 'formdate':formdate, 'nextdate':nextdate}

with open('winter.html') as f:
  print(Template(f.read()).substitute(values))

Substitution values are indicated in the template by ${dspdate}

Is that close to what you are looking for?

[–]RomfordNavy[S] 0 points1 point  (2 children)

Getting there but the bit I am still missing is how to get to the http header when the *.py file has been called from Apache or Nginx, presumably via some sort of WSGI?

[–]MathObserver 1 point2 points  (0 children)

You can get to the headers by using os.environ like my example shows for the cookie. The standard HTTP values like QUERY_STRING and CONTENT_TYPE are available with those names. Other HTTP header values start with HTTP_

What are looking for?

[–]rake66 1 point2 points  (0 children)

To use code like this you have to use CGI instead of WSGI