Cookie: Not understanding again

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mosscliffe

    #1

    Cookie: Not understanding again

    I have the following code, which I thought would create a cookie, if
    one did not exist and on the html form being sent to the server, it
    would be availabe for interrogation, when the script is run a second
    time.

    It seems to me there is something basic missing in that I should need
    to tell the server I am sending a cookie, but all the docs I have read
    imply it is done automatically, if one creates a 'Cookie.SimpleC ookie'

    I know my understanding is poor of web server logic, but if anyone can
    help, I will be most grateful.

    This may be more to do with Web Server behaviour than python
    programming, but surely there is a way of doing this in python.

    Richard

    #!/usr/bin/env python

    import Cookie, os
    import cgitb;cgitb.ena ble()

    def getCookie():
    c = Cookie.SimpleCo okie()
    if 'HTTP_COOKIE' in os.environ:
    c.load(os.envir on['HTTP_COOKIE'])
    print "Found a Cookie", c, "<BR>"
    c['mysession'].value += 1
    print "<HR>"
    return c
    else:
    c['mysession'] = 45
    print "No Cookie Found so setting an initial value for a Cookie", c
    print "<HR>"
    return c

    if __name__ == '__main__':

    print "Content-type: text/html\n\n"
    myCookie = getCookie()
    #Print all Environment variables
    for k, v in os.environ.item s():
    print k, "=", v, "<BR>"
    print "<HR>"

    print """
    <form method="get">
    <input type="text" name="user" value="">
    </form>
    """

  • mosscliffe

    #2
    Re: Cookie: Not understanding again

    On 1 Jun, 15:49, mosscliffe <mcl.off...@goo glemail.comwrot e:
    I have the following code, which I thought would create a cookie, if
    one did not exist and on the html form being sent to the server, it
    would be availabe for interrogation, when the script is run a second
    time.
    >
    It seems to me there is something basic missing in that I should need
    to tell the server I am sending a cookie, but all the docs I have read
    imply it is done automatically, if one creates a 'Cookie.SimpleC ookie'
    >
    I know my understanding is poor of web server logic, but if anyone can
    help, I will be most grateful.
    >
    This may be more to do with Web Server behaviour than python
    programming, but surely there is a way of doing this in python.
    >
    Richard
    >
    #!/usr/bin/env python
    >
    import Cookie, os
    import cgitb;cgitb.ena ble()
    >
    def getCookie():
    c = Cookie.SimpleCo okie()
    if 'HTTP_COOKIE' in os.environ:
    c.load(os.envir on['HTTP_COOKIE'])
    print "Found a Cookie", c, "<BR>"
    c['mysession'].value += 1
    print "<HR>"
    return c
    else:
    c['mysession'] = 45
    print "No Cookie Found so setting an initial value for a Cookie", c
    print "<HR>"
    return c
    >
    if __name__ == '__main__':
    >
    print "Content-type: text/html\n\n"
    myCookie = getCookie()
    #Print all Environment variables
    for k, v in os.environ.item s():
    print k, "=", v, "<BR>"
    print "<HR>"
    >
    print """
    <form method="get">
    <input type="text" name="user" value="">
    </form>
    """
    Forgot to add running python 2.3.4 on a hosted Apache server

    Comment

    • David Wahler

      #3
      Re: Cookie: Not understanding again

      On Jun 1, 3:49 pm, mosscliffe <mcl.off...@goo glemail.comwrot e:
      I have the following code, which I thought would create a cookie, if
      one did not exist and on the html form being sent to the server, it
      would be availabe for interrogation, when the script is run a second
      time.
      >
      It seems to me there is something basic missing in that I should need
      to tell the server I am sending a cookie, but all the docs I have read
      imply it is done automatically, if one creates a 'Cookie.SimpleC ookie'
      >
      I know my understanding is poor of web server logic, but if anyone can
      help, I will be most grateful.
      >
      This may be more to do with Web Server behaviour than python
      programming, but surely there is a way of doing this in python.
      >
      <snip code>

      The cookie values have to be sent to the browser as part of the
      response headers. (Modifying the Cookie object doesn't do this for you
      -- the Cookie instance just acts as a container.) In other words,
      something like this:

      c = Cookie.SimpleCo okie()
      c.load(os.envir on['HTTP_COOKIE'])
      # ...modify cookie object...
      print "Content-Type: text/html"
      print c
      print
      # send the HTML text

      -- David


      Comment

      • mosscliffe

        #4
        Re: Cookie: Not understanding again

        On 2 Jun, 01:13, David Wahler <dwah...@gmail. comwrote:
        On Jun 1, 3:49 pm, mosscliffe <mcl.off...@goo glemail.comwrot e:I have the following code, which I thought would create a cookie, if
        one did not exist and on the html form being sent to the server, it
        would be availabe for interrogation, when the script is run a second
        time.
        >
        It seems to me there is something basic missing in that I should need
        to tell the server I am sending a cookie, but all the docs I have read
        imply it is done automatically, if one creates a 'Cookie.SimpleC ookie'
        >
        I know my understanding is poor of web server logic, but if anyone can
        help, I will be most grateful.
        >
        This may be more to do with Web Server behaviour than python
        programming, but surely there is a way of doing this in python.
        >
        <snip code>
        >
        David,

        Thanks for your help.

        I spent all last night trying to get it to work, but I can not work
        out how to force my cookie into the response header. The most
        annoying part is on one occasion it did and the cookie sat there until
        I killed the session. By the time I discovered 'pageinfo' add-in for
        firefox, the cookie had gone.

        I have read a lot about cookies, but the grey matter is very slow at
        absorbing nowadays. I was wondering if I should add the 'path=/' to
        my cookie, as I am using a host that has numbers for the IP address
        and my domain name when used, is via a frame. Although the python bit
        to display the contents (when it did work), did not show any value for
        'path'.

        Could I create the response header in python and get it executed as
        part of my form submission ?

        Obviously, still struggling.

        Richard
        The cookie values have to be sent to the browser as part of the
        response headers. (Modifying the Cookie object doesn't do this for you
        -- the Cookie instance just acts as a container.) In other words,
        something like this:
        >
        c = Cookie.SimpleCo okie()
        c.load(os.envir on['HTTP_COOKIE'])
        # ...modify cookie object...
        print "Content-Type: text/html"
        print c
        print
        # send the HTML text
        >
        -- David

        Comment

        • David Wahler

          #5
          Re: Cookie: Not understanding again

          On Jun 2, 9:32 am, mosscliffe <mcl.off...@goo glemail.comwrot e:
          David,
          >
          Thanks for your help.
          >
          I spent all last night trying to get it to work, but I can not work
          out how to force my cookie into the response header. The most
          annoying part is on one occasion it did and the cookie sat there until
          I killed the session. By the time I discovered 'pageinfo' add-in for
          firefox, the cookie had gone.
          Your CGI script needs to produce output of the form:

          Header-1: ...
          Header-2: ...

          response data goes here...

          Each line before the blank line is an HTTP header. To send cookies to
          the browser, you need to generate a header of the form:

          Set-Cookie: name=value

          Printing the SimpleCookie object does this for you.
          I have read a lot about cookies, but the grey matter is very slow at
          absorbing nowadays. I was wondering if I should add the 'path=/' to
          my cookie, as I am using a host that has numbers for the IP address
          and my domain name when used, is via a frame. Although the python bit
          to display the contents (when it did work), did not show any value for
          'path'.
          If you specify a path attribute, then the browser will only send the
          cookie on requests that begin with that path. If you don't, it
          defaults to the URL of the page that generates the cookie. For a
          single test script like this, there's no need to bother with it, but
          you can set it like this:
          >>cookie["someCookieName "]["path"] = "/path/to/whatever"
          Could I create the response header in python and get it executed as
          part of my form submission ?
          >
          Obviously, still struggling.
          Here's a simple, complete example to get you started, based loosely on
          your original code:

          ############### ############### ############### ############### ##########

          import Cookie, os
          import cgitb; cgitb.enable()

          def getCookie():
          c = Cookie.SimpleCo okie()
          if 'HTTP_COOKIE' in os.environ:
          c.load(os.envir on['HTTP_COOKIE'])

          if os.environ['QUERY_STRING'] == 'reset' or 'mysession' not in c:
          c['mysession'] = 0
          else:
          c['mysession'] = int(c['mysession'].value) + 1

          return c

          if __name__ == '__main__':

          print "Content-type: text/html"
          myCookie = getCookie()
          print myCookie

          if os.environ['QUERY_STRING'] == 'reset':
          print "Status: 302 Moved"
          print "Location:" , os.environ['SCRIPT_NAME']
          print
          else:
          print
          print 'Current Value: ', myCookie['mysession'].value
          print '<br><a href="?reset">R eset Counter</a>'

          ############### ############### ############### ############### ##########

          Hope this helps!

          -- David

          Comment

          • mosscliffe

            #6
            Re: Cookie: Not understanding again

            On 2 Jun, 11:50, David Wahler <dwah...@gmail. comwrote:
            On Jun 2, 9:32 am, mosscliffe <mcl.off...@goo glemail.comwrot e:
            >
            David,
            >
            Thanks for your help.
            >
            I spent all last night trying to get it to work, but I can not work
            out how to force mycookieinto the response header. The most
            annoying part is on one occasion it did and thecookiesat there until
            I killed the session. By the time I discovered 'pageinfo' add-in for
            firefox, thecookiehad gone.
            >
            Your CGI script needs to produce output of the form:
            >
            Header-1: ...
            Header-2: ...
            >
            response data goes here...
            >
            Each line before the blank line is an HTTP header. To send cookies to
            the browser, you need to generate a header of the form:
            >
            Set-Cookie: name=value
            >
            Printing the SimpleCookie object does this for you.
            >
            I have read a lot about cookies, but the grey matter is very slow at
            absorbing nowadays. I was wondering if I should add the 'path=/' to
            mycookie, as I am using a host that has numbers for the IP address
            and my domain name when used, is via a frame. Although the python bit
            to display the contents (when it did work), did not show any value for
            'path'.
            >
            If you specify a path attribute, then the browser will only send thecookieon requests that begin with that path. If you don't, it
            defaults to the URL of the page that generates thecookie. For a
            single test script like this, there's no need to bother with it, but
            you can set it like this:
            >
            >>cookie["someCookieName "]["path"] = "/path/to/whatever"
            Could I create the response header in python and get it executed as
            part of my form submission ?
            >
            Obviously, still struggling.
            >
            Here's a simple, complete example to get you started, based loosely on
            your original code:
            >
            ############### ############### ############### ############### ##########
            >
            importCookie, os
            import cgitb; cgitb.enable()
            >
            def getCookie():
            c =Cookie.SimpleC ookie()
            if 'HTTP_COOKIE' in os.environ:
            c.load(os.envir on['HTTP_COOKIE'])
            >
            if os.environ['QUERY_STRING'] == 'reset' or 'mysession' not in c:
            c['mysession'] = 0
            else:
            c['mysession'] = int(c['mysession'].value) + 1
            >
            return c
            >
            if __name__ == '__main__':
            >
            print "Content-type: text/html"
            myCookie = getCookie()
            print myCookie
            >
            if os.environ['QUERY_STRING'] == 'reset':
            print "Status: 302 Moved"
            print "Location:" , os.environ['SCRIPT_NAME']
            print
            else:
            print
            print 'Current Value: ', myCookie['mysession'].value
            print '<br><a href="?reset">R eset Counter</a>'
            >
            ############### ############### ############### ############### ##########
            >
            Hope this helps!
            >
            -- David
            David,

            Thank you very much for your detailed reply. As soon as I get a
            chance to try it all out, I will let you know how I get on.

            Thanks again

            Richard

            Comment

            Working...