Re: Cookies not showing up in environ

2018-07-26 Thread abc abc
Yes, this was the problem. At some point early in my attempts I got the idea 
that linking up Django to a production web server required writing a separate 
wsgi.py script. Wrong. Replaced the wsgi.py with the default Django wsgi.py for 
the project and everything seems resolved. Thank you.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-21 Thread Jon Ribbens
On 2018-07-21, abc abc  wrote:
>> I think one of the main issues is that you don't seem to have decided
>> whether you're writing a WSGI application or a Django application.
>
> Yes, I suppose I thought Django had to use wsgi to process requests,
> I didn't know there were 'two' options here.

Django does use wsgi to process requests. But if you've written
an application in Django, you don't need to then write it again
without Django - Django has its own wsgi app that will call the
Django app for you.

> Does your example represent one or the other?

Both - I gave two alternatives, and labelled each. You only need one.

> project_folder/app/wsgi.py
>
> def application(environ, start_response):
> start_response('200 OK',
>[('Content-Type', 'text/plain; charset=utf-8')])
> return [environ.get('HTTP_COOKIE', '').encode('utf-8')]

You should delete that and change it back to whatever 'django
startproject' gave you originally for wsgi.py. If you're writing
a django app then you don't need to also write a wsgi app.

> Cleared all cookies in the browser, and went to
> localhost:8000/cookies which loaded with no error in the console or
> browser. No cookies were created in the browser preferences.

Well, you're not setting any cookies. You'd need to do something like:

 def cookies(request):
 response = HttpResponse(repr(request.COOKIES),
 'text/plain; charset=utf-8')
 response.set_cookie('mycookie', 'value')
 return response

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-21 Thread abc abc
> I think one of the main issues is that you don't seem to have decided
> whether you're writing a WSGI application or a Django application.

Yes, I suppose I thought Django had to use wsgi to process requests, I didn't 
know there were 'two' options here. Does your example represent one or the 
other?

My mistake, I should have mentioned I was using Django 1.11 so I had to use url 
instead of path in urls.py. Below, I have tried to provide as much information 
as possible.

I tried with just the files you provided with slight modifications:

project_folder/app/views.py:

from django.http import HttpResponse

def cookies(request):
return HttpResponse(repr(request.COOKIES),
'text/plain; charset=utf-8')

def index(request):
print 'INDEX: '
print request.COOKIE
return HttpResponse('this is the response')


project_folder/app/urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
url('cookies', views.cookies),
url('index', views.index),
]


project_folder/app/wsgi.py

def application(environ, start_response):
start_response('200 OK',
   [('Content-Type', 'text/plain; charset=utf-8')])
return [environ.get('HTTP_COOKIE', '').encode('utf-8')]


>From the project folder, I ran 'python manage.py runserver'

Cleared all cookies in the browser, and went to localhost:8000/cookies which 
loaded with no error in the console or browser. No cookies were created in the 
browser preferences.

I tried refreshing the browser for localhost:8000/index but still no cookies. 
Also, for views.py, the print statement 'INDEX: ' did not appear in the 
console, nor did the http response string I returned (in either browser or 
console). So something is not getting processed right. In my previous attempts 
(to your example code) I had no issue rendering templates and html. The only 
thing that didn't seem to work were the cookies.


Console Output:

Performing system checks...

System check identified no issues (0 silenced).
July 21, 2018 - 20:47:24
Django version 1.11.13, using settings 'app.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[21/Jul/2018 20:47:34] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:34] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:47:54] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /cookies HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:48:07] "GET /index HTTP/1.1" 200 0
[21/Jul/2018 20:48:08] "GET /favicon.ico HTTP/1.1" 200 0
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-21 Thread abc abc
> I think one of the main issues is that you don't seem to have decided
> whether you're writing a WSGI application or a Django application.

Yes, I suppose I thought Django had to use wsgi to process requests, I didn't 
know there were 'two' options here. Does your example represent one or the 
other?

My mistake, I should have mentioned I was using Django 1.11 so I had to use url 
instead of path in urls.py. Below, I have tried to provide as much information 
as possible.

I tried with just the files you provided with slight modifications:

project_folder/app/views.py:

from django.http import HttpResponse

def cookies(request):
return HttpResponse(repr(request.COOKIES),
'text/plain; charset=utf-8')

def index(request):
print 'INDEX: '
print request.COOKIE
return HttpResponse('this is the response')


project_folder/app/urls.py:

from django.conf.urls import url
from . import views

urlpatterns = [
url('cookies', views.cookies),
url('index', views.index),
]


project_folder/app/wsgi.py

def application(environ, start_response):
start_response('200 OK',
   [('Content-Type', 'text/plain; charset=utf-8')])
return [environ.get('HTTP_COOKIE', '').encode('utf-8')]


>From the project folder, I ran 'python manage.py runserver'

Cleared all cookies in the browser, and went to localhost:8000/cookies which 
loaded with no error in the console or browser. No cookies were created in the 
browser preferences.

I tried refreshing the browser for localhost:8000/index but still no cookies. 
Also, for views.py, the print statement 'INDEX: ' did not appear in the 
console, nor did the http response string I returned (in either browser or 
console). So something is not getting processed right. In my previous attempts 
(to your example code) I had no issue rendering templates and html. The only 
thing that didn't seem to work were the cookies.


Console Output:

Performing system checks...

System check identified no issues (0 silenced).
July 21, 2018 - 20:47:24
Django version 1.11.13, using settings 'omicron.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[21/Jul/2018 20:47:34] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:34] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:47:54] "GET / HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /cookies HTTP/1.1" 200 0
[21/Jul/2018 20:47:58] "GET /favicon.ico HTTP/1.1" 200 0
[21/Jul/2018 20:48:07] "GET /index HTTP/1.1" 200 0
[21/Jul/2018 20:48:08] "GET /favicon.ico HTTP/1.1" 200 0
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-21 Thread Jon Ribbens
On 2018-07-20, abc abc  wrote:
> Well, I'm so messed up between so many sources and tutorials I don't
> know which way is up. 

I think one of the main issues is that you don't seem to have decided
whether you're writing a WSGI application or a Django application.

WSGI:

def application(environ, start_response):
start_response('200 OK',
   [('Content-Type', 'text/plain; charset=utf-8')])
return [environ.get('HTTP_COOKIE', '').encode('utf-8')]


Django:

views.py:

from django.http import HttpResponse

def cookies(request):
return HttpResponse(repr(request.COOKIES),
'text/plain; charset=utf-8')

urls.py:

from django.urls import path

from . import views

urlpatterns = [
path('cookies', views.cookies),
]

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-20 Thread abc abc
Well, I'm so messed up between so many sources and tutorials I don't know which 
way is up. 

References to environ variable:
https://www.python.org/dev/peps/pep-0333/
https://stackoverflow.com/questions/16774952/wsgi-whats-the-purpose-of-start-response-function

I read that I have to have a file project/project/wsgi.py that has a function 
called 'application' that gets called; still a little fuzzy on whose calling it 
and passing these parameters. I understood that the cookies should be in 
HTTP_COOKIES inside the environ hash... they're not. From wsgi.py I call a 
function in my view which returns a response object. Sorry if my parameters 
don't quite match up, I've been rearranging things while trying to get this to 
work; it should just be environ and start_response in wsgi:application().

If I try to access the request object, it is None, and I'm not sure how I would 
get it if it's not coming form wsgi.py:application...

My previous example is bad since they are passing 'status' to the 
start_response which I don't know how got created. TBH: I've spent days and 
days on this in different forums and am not getting much help or progress. I 
think I have made some sort of bad assumption in the beginning which is 
blocking me now.

I don't normally like to ask for handouts, but can you please point me to a 
dead simple setup with wsgi.py and view.py that can handle cookies... just 
simply setting them and reading them?
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-20 Thread Peter J. Holzer
On 2018-07-20 12:39:38 -0700, abc abc wrote:
> I am trying the simplest of examples below to set and read a cookie. I
> am using the Django framework, but I have also tried with vanilla
> python cookies and os.environ. I have posted elsewhere, but this
> hasn't gotten much attention, so I'd really appreciate any help.
> 
> Thinking at this point it may be something to with the server, not
> python. I have tried uwsgi/nginx, gunicorn/nginx, django development
> server with no success.
> 
> view.py:
> 
> def index(environ, start_response, request):
> if not 'HTTP_COOKIE' in environ:

The first argument to a Django view function is the request object,
not anything one could plausibly name "environ". See the django docs on
how to access the fields of a request.

hp

-- 
   _  | Peter J. Holzer| we build much bigger, better disasters now
|_|_) || because we have much more sophisticated
| |   | h...@hjp.at | management tools.
__/   | http://www.hjp.at/ | -- Ross Anderson 


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies not showing up in environ

2018-07-20 Thread Calvin Spealman
You can read cookies from the request via the request.COOKIES dictionary.
See the documentation here:
https://docs.djangoproject.com/en/2.0/ref/request-response/#django.http.HttpRequest.COOKIES

You won't find them in an environment variable, which is shared
process-wide and across all requests, because they are bound to each
request. This is not a CGI script, so those HTTP_* environment variables
you seem to expect won't be here.

On Fri, Jul 20, 2018 at 3:39 PM, abc abc  wrote:

> I am trying the simplest of examples below to set and read a cookie. I am
> using the Django framework, but I have also tried with vanilla python
> cookies and os.environ. I have posted elsewhere, but this hasn't gotten
> much attention, so I'd really appreciate any help.
>
> Thinking at this point it may be something to with the server, not python.
> I have tried uwsgi/nginx, gunicorn/nginx, django development server with no
> success.
>
> view.py:
>
> def index(environ, start_response, request):
> if not 'HTTP_COOKIE' in environ:
> response = HttpResponse("hello")
> response.set_cookie('user_agreement', 'cookie text', domain='.
> mysite.com')
> return response
> else:
> print environ['HTTP_COOKIE']
>
> The webpage just prints 'hello' and never reaches the else not matter how
> many times I refresh the page. There are no cookies in the browser ever
> either.
>
> Am I missing some middleware? I remember some answers to cookie problems
> related to the order of their middleware configurations. Here's mine:
>
> MIDDLEWARE = [
> 'django.middleware.security.SecurityMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware',
> 'django.middleware.common.CommonMiddleware',
> 'django.middleware.csrf.CsrfViewMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.messages.middleware.MessageMiddleware',
> 'django.middleware.clickjacking.XFrameOptionsMiddleware',
> ]
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-31 Thread Victor Subervi
On Thu, Dec 31, 2009 at 1:10 AM, Carsten Haese carsten.ha...@gmail.comwrote:

 Victor Subervi wrote:
  You know, neither one of those tutorials I followed gave clear, or any,
  instruction about putting a
  print cookie
  statement in the header! How misleading!

 Don't blame the tutorials for your failure to read them carefully. The
 tutorial you mentioned
 (http://www.doughellmann.com/PyMOTW/Cookie/index.html) says the
 following right at the beginning: The Cookie module defines classes for
 parsing and creating HTTP cookie *headers*. [Emphasis mine.]

 Further down it says: The output is a valid Set-Cookie *header* ready
 to be passed to the client as part of the HTTP response. [Emphasis
 mine.] You even copy-and-pasted a snippet containing that very sentence
 into an earlier post on this thread, so how you can now claim that the
 tutorial didn't mention this is quite beyond me.


I see your point. It wasn't, however, clear to me. Probably my own problem.
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-31 Thread Victor Subervi
I'm curious. Are there other instances where code needs to be inserted into
the header as in the print cookie to get it to be baked on the user's PC?
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Tue, Dec 29, 2009 at 3:43 PM, Carsten Haese carsten.ha...@gmail.comwrote:

 Victor Subervi wrote:
  Hi;
  I have these lines:
 
cookie = os.environ.get('HTTP_COOKIE')
if not cookie:
  cookie = Cookie.SimpleCookie()
  cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
  cookie['lastvisit'] = str(time.time())
  cookie['lastvisit']['expires'] = cExpires
  cookie['lastvisit']['path'] = cPath
  cookie['lastvisit']['comment'] = cComment
  cookie['lastvisit']['domain'] = cDomain
  cookie['lastvisit']['max-age'] = cMaxAge
  cookie['lastvisit']['version'] = cVersion
  cookieFlag = 'new'
else:
  cookie = Cookie.SimpleCookie(cookie)
  cookieFlag = 'old'
print '''Content-Type: text/html\r\n
  !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
  html
  '''
print cookieFlag
 
  cookieFlag prints 'new'. Every time. Even when I refresh. I've imported
  Cookie. I've followed the tutorials :-} I've already been through my
  problem with trying to find the cookies on this computer. The fact that
  I'm using gmail is proof that cookies are enabled. I have also tried:
  cookie = os.environ.has_key('HTTP_COOKIE')
  Please advise.

 You apparently haven't followed the tutorials carefully enough. You do
 know that a cookie is a piece of information that's stored in your
 browser, don't you? So tell me, which of the above lines of code do you
 suppose is responsible for informing your browser of the cookie's contents?


This one:

cookie = SimpleCookie()

In the tutorial found here, for example:

http://www.doughellmann.com/PyMOTW/Cookie/index.html

I read the following:

snip
Cookies are used as state management, and as such as usually set by the
server to be stored and returned by the client. The most trivial example of
creating a cookie looks something like:

import Cookie

c = Cookie.SimpleCookie()
c['mycookie'] = 'cookie_value'
print c

The output is a valid Set-Cookie header ready to be passed to the client as
part of the HTTP response:

$ python Cookie_setheaders.py
Set-Cookie: mycookie=cookie_value
/snip

Do not the printout words Set-cookie indicate the cookie has been set?
Furthermore, none of the tutorials I have found state anything differently.
Another example is found here:

http://webpython.codepoint.net/cgi_set_the_cookie

Please advise.
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Carsten Haese
Victor Subervi wrote:
 On Tue, Dec 29, 2009 at 3:43 PM, Carsten Haese carsten.ha...@gmail.com
 mailto:carsten.ha...@gmail.com wrote:
 You apparently haven't followed the tutorials carefully enough. You do
 know that a cookie is a piece of information that's stored in your
 browser, don't you? So tell me, which of the above lines of code do you
 suppose is responsible for informing your browser of the cookie's
 contents?
 
 
 This one:
 
 cookie = SimpleCookie()
 
 In the tutorial found here, for example:
 
 http://www.doughellmann.com/PyMOTW/Cookie/index.html
 
 I read the following:
 
 snip
 Cookies are used as state management, and as such as usually set by the
 server to be stored and returned by the client. The most trivial example
 of creating a cookie looks something like:
 
 import Cookie
 
 c = Cookie.SimpleCookie()
 c['mycookie'] = 'cookie_value'
 print c
 
 The output is a valid Set-Cookie header ready to be passed to the client
 as part of the HTTP response:
 
 $ python Cookie_setheaders.py
 Set-Cookie: mycookie=cookie_value
 /snip
 
 Do not the printout words Set-cookie indicate the cookie has been set?

That's exactly what that printout indicates, and ONLY that printout
indicates that. Without that printout, no cookie will be set in the
browser, because that printout is what transports the contents of the
cookie to the browser. However, the line c = Cookie.SimpleCookie()
is not responsible for producing that printout, which can be tested
easily in a Python command line:

py import Cookie
py c = Cookie.SimpleCookie()
py

As you can see, no printout is generated.

By the way, expecting a line of code to produce content that is
established in lines of codes that follow it requires a mental execution
model of Python that involves magic, mind reading, or time travel, none
of which are features Python is known for.

So, guess again. The trivial example has three more lines of code. One
of them is unlike any line you have in your code. That's the line
responsible for producing the Set-Cookie header, and that's the line
you're missing.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 9:56 AM, Carsten Haese carsten.ha...@gmail.comwrote:

 So, guess again. The trivial example has three more lines of code. One
 of them is unlike any line you have in your code. That's the line
 responsible for producing the Set-Cookie header, and that's the line
 you're missing.


So you're suggesting I somehow execute this code:
python Cookie_setheaders.py
in my script? I presume I'd change Cookie_setheaders.py to my file name.
That doesn't make sense to me.

I'm sorry but I'm lost. The page on which you said had the missing line:
http://www.doughellmann.com/PyMOTW/Cookie/index.html
has the following code:

import Cookie
c = Cookie.SimpleCookie()
c['mycookie'] = 'cookie_value'
print c

All of which is in my code. The tutorial gave this printout:

$ python Cookie_setheaders.py
Set-Cookie: mycookie=cookie_value

It also has this code:

import Cookie
import datetime

def show_cookie(c):
print c
for key, morsel in c.iteritems():
print
print 'key =', morsel.key
print '  value =', morsel.value
print '  coded_value =', morsel.coded_value
for name in morsel.keys():
if morsel[name]:
print '  %s = %s' % (name, morsel[name])

which, when I tried it, printed nothing at all. Not a very good tutorial :(

This page
http://webpython.codepoint.net/cgi_set_the_cookie
has the following code:

#!/usr/bin/env python
import time
print 'Set-Cookie: lastvisit=' + str(time.time());
print 'Content-Type: text/html\n'
print 'htmlbody'
print 'Server time is', time.asctime(time.localtime())
print '/body/html'



#!/usr/bin/env python
import time, Cookie
cookie = Cookie.SimpleCookie()
cookie['lastvisit'] = str(time.time())
print cookie
print 'Content-Type: text/html\n'
print 'htmlbody'
print 'Server time is', time.asctime(time.localtime())
print '/body/html'

all of which is included in my code (except for extraneous print
statements). Here again is my code:

#! /usr/bin/python

import string
import cgitb; cgitb.enable()
import MySQLdb
import cgi
import sys,os
sys.path.append(os.getcwd())
from login import login
import datetime, Cookie, random
from particulars import title
from templateFrame import top, bottom
from particulars import myCookie
import time
import fpformat
from sets import Set
from particulars import ourOptions

def cart():
  print '''Content-Type: text/html\r\n
!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
html
'''
  cookie = os.environ.has_key('HTTP_COOKIE')
  if not cookie:
cookie = Cookie.SimpleCookie()
cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
cookie['lastvisit'] = str(time.time())
cookie['lastvisit']['expires'] = cExpires
cookie['lastvisit']['path'] = cPath
cookie['lastvisit']['comment'] = cComment
cookie['lastvisit']['domain'] = cDomain
cookie['lastvisit']['max-age'] = cMaxAge
cookie['lastvisit']['version'] = cVersion
cookie['mycookie'] = 'mycookie'
cookieFlag = 'new'
  else:
cookie = Cookie.SimpleCookie(cookie)
cookieFlag = 'old'
#  Don't know what to do with this. It's for when client won't accept
cookies
#  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
#  session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
#  session['lastvisit'] = repr(time.time())
#  session.close()
  print cookieFlag
  cookie.load(cookie)
  print cookie
 ...

Please advise.
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Carsten Haese
Victor Subervi wrote:
 Here again is my code:
 
 #! /usr/bin/python
 
 import string
 import cgitb; cgitb.enable()
 import MySQLdb
 import cgi
 import sys,os
 sys.path.append(os.getcwd())
 from login import login
 import datetime, Cookie, random
 from particulars import title
 from templateFrame import top, bottom
 from particulars import myCookie
 import time
 import fpformat
 from sets import Set
 from particulars import ourOptions
 
 def cart():
   print '''Content-Type: text/html\r\n
 !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
 html
 '''
   cookie = os.environ.has_key('HTTP_COOKIE')
   if not cookie:
 cookie = Cookie.SimpleCookie()
 cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
 cookie['lastvisit'] = str(time.time())
 cookie['lastvisit']['expires'] = cExpires
 cookie['lastvisit']['path'] = cPath
 cookie['lastvisit']['comment'] = cComment
 cookie['lastvisit']['domain'] = cDomain
 cookie['lastvisit']['max-age'] = cMaxAge
 cookie['lastvisit']['version'] = cVersion
 cookie['mycookie'] = 'mycookie'
 cookieFlag = 'new'
   else:
 cookie = Cookie.SimpleCookie(cookie)
 cookieFlag = 'old'
 #  Don't know what to do with this. It's for when client won't accept
 cookies
 #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
 #  session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
 #  session['lastvisit'] = repr(time.time())
 #  session.close()
   print cookieFlag
   cookie.load(cookie)
   print cookie
  ...
 
 Please advise.

Pardon me for not being able to read your mind. The code you're now
posting is significantly more code than your first post included. The
line that I had determined to be missing is in fact not missing, you
just didn't bother to post your actual code.

Now that you've posted your actual code, I can see that the actual
problem is that the line is in the wrong place.

The line in question is print cookie, and it's responsible for
printing a Set-Cookie *HEADER*, which is a fact that at least one of
the tutorials you pointed out has mentioned. You're printing it where it
doesn't belong, in the middle of your HTML output. You need to
reorganize your print statements such that the Set-Cookie header is
printed in the header of your script's output.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Steve Holden
Carsten Haese wrote:
 Victor Subervi wrote:
[...]
 Pardon me for not being able to read your mind. The code you're now
 posting is significantly more code than your first post included. The
 line that I had determined to be missing is in fact not missing, you
 just didn't bother to post your actual code.
 
 Now that you've posted your actual code, I can see that the actual
 problem is that the line is in the wrong place.
 
 The line in question is print cookie, and it's responsible for
 printing a Set-Cookie *HEADER*, which is a fact that at least one of
 the tutorials you pointed out has mentioned. You're printing it where it
 doesn't belong, in the middle of your HTML output. You need to
 reorganize your print statements such that the Set-Cookie header is
 printed in the header of your script's output.
 
I will point out again, without the least expectation that it will do
any good, that the reason this problem has occurred is that Victor
simply refuses to take the time to absorb the principles of what he is
attempting before attempting it.

This is what I call programming with a trowel - take a chunk of code
from here and a chunk from there, splash a little bit of cement logic
around to glue everything together, and hope it works. The advanced part
of the strategy here is to then throw the resulting mess at an
accommodating newsgroup in the hope that the readers will turn it into
working code.

It's as though he had taken the front half of a motor cycle and the back
half of a saloon car, welded them together without bothering to make any
electrical or fuel line connections, and then complaining that the
vehicle doesn't work. Of course it doesn't, because there is no
underlying understanding of the principles involved.

The interesting thing is that the web site is apparently being put
together to sell something. I have no idea what that something might be,
but I am as sure as I can be that I don't want to buy it, just from the
insight I have gained into the vendor's thought processes from the
various threads I have recently been involved in.

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 11:54 AM, Carsten Haese carsten.ha...@gmail.comwrote:

 Victor Subervi wrote:
  Here again is my code:
 
  #! /usr/bin/python
 
  import string
  import cgitb; cgitb.enable()
  import MySQLdb
  import cgi
  import sys,os
  sys.path.append(os.getcwd())
  from login import login
  import datetime, Cookie, random
  from particulars import title
  from templateFrame import top, bottom
  from particulars import myCookie
  import time
  import fpformat
  from sets import Set
  from particulars import ourOptions
 
  def cart():
print '''Content-Type: text/html\r\n
  !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
  html
  '''
cookie = os.environ.has_key('HTTP_COOKIE')
if not cookie:
  cookie = Cookie.SimpleCookie()
  cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
  cookie['lastvisit'] = str(time.time())
  cookie['lastvisit']['expires'] = cExpires
  cookie['lastvisit']['path'] = cPath
  cookie['lastvisit']['comment'] = cComment
  cookie['lastvisit']['domain'] = cDomain
  cookie['lastvisit']['max-age'] = cMaxAge
  cookie['lastvisit']['version'] = cVersion
  cookie['mycookie'] = 'mycookie'
  cookieFlag = 'new'
else:
  cookie = Cookie.SimpleCookie(cookie)
  cookieFlag = 'old'
  #  Don't know what to do with this. It's for when client won't accept
  cookies
  #  sessionDir = os.environ['DOCUMENT_ROOT'] + '/tmp/.session'
  #  session = shelve.open(sessionDir + '/sess_' + sid, writeback=True)
  #  session['lastvisit'] = repr(time.time())
  #  session.close()
print cookieFlag
cookie.load(cookie)
print cookie
   ...
 
  Please advise.

 Pardon me for not being able to read your mind. The code you're now
 posting is significantly more code than your first post included. The
 line that I had determined to be missing is in fact not missing, you
 just didn't bother to post your actual code.


As my British father would have said, you're a gentleman and a scholar...



 Now that you've posted your actual code, I can see that the actual
 problem is that the line is in the wrong place.

 The line in question is print cookie, and it's responsible for
 printing a Set-Cookie *HEADER*, which is a fact that at least one of
 the tutorials you pointed out has mentioned. You're printing it where it
 doesn't belong, in the middle of your HTML output. You need to
 reorganize your print statements such that the Set-Cookie header is
 printed in the header of your script's output.


I've revised the code thus:

  cookie = os.environ.has_key('HTTP_COOKIE')
  if not cookie:
cookie = Cookie.SimpleCookie()
cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
cookie['lastvisit'] = str(time.time())
cookie['lastvisit']['expires'] = cExpires
cookie['lastvisit']['path'] = cPath
cookie['lastvisit']['comment'] = cComment
cookie['lastvisit']['domain'] = cDomain
cookie['lastvisit']['max-age'] = cMaxAge
cookie['lastvisit']['version'] = cVersion
print cookie
cookieFlag = 'new'

Unfortunately, this still prints new for the cookieFlag no matter how many
times I refresh. Please advise.
TIA,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 12:19 PM, Steve Holden st...@holdenweb.com wrote:

 Carsten Haese wrote:
 I will point out again, without the least expectation that it will do
 any good, that the reason this problem has occurred is that Victor
 simply refuses to take the time to absorb the principles of what he is
 attempting before attempting it.

 This is what I call programming with a trowel - take a chunk of code
 from here and a chunk from there, splash a little bit of cement logic
 around to glue everything together, and hope it works. The advanced part
 of the strategy here is to then throw the resulting mess at an
 accommodating newsgroup in the hope that the readers will turn it into
 working code.

 It's as though he had taken the front half of a motor cycle and the back
 half of a saloon car, welded them together without bothering to make any
 electrical or fuel line connections, and then complaining that the
 vehicle doesn't work. Of course it doesn't, because there is no
 underlying understanding of the principles involved.

 The interesting thing is that the web site is apparently being put
 together to sell something. I have no idea what that something might be,
 but I am as sure as I can be that I don't want to buy it, just from the
 insight I have gained into the vendor's thought processes from the
 various threads I have recently been involved in.


Comments from a left-brain thinker without any concept of how difficult it
is for a right-brain thinker to think like you. Care to compare poetry? I'd
bury you.


 regards


Regards what?
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Carsten Haese
Victor Subervi wrote:
 I've revised the code thus:
 
   cookie = os.environ.has_key('HTTP_COOKIE')
   if not cookie:
 cookie = Cookie.SimpleCookie()
 cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
 cookie['lastvisit'] = str(time.time())
 cookie['lastvisit']['expires'] = cExpires
 cookie['lastvisit']['path'] = cPath
 cookie['lastvisit']['comment'] = cComment
 cookie['lastvisit']['domain'] = cDomain
 cookie['lastvisit']['max-age'] = cMaxAge
 cookie['lastvisit']['version'] = cVersion
 print cookie
 cookieFlag = 'new'
  
 Unfortunately, this still prints new for the cookieFlag no matter how
 many times I refresh. Please advise.

That tells me nothing, because once again you're not posting your
complete code.

Anyway, the likely answer is that you guessed incorrectly. As I said
before, you need to make sure that the cookie is printed as part of the
page headers. I'll give you one last hint: The page header is where
you're printing the Content-type line.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Carsten Haese
Victor Subervi wrote:
 Comments from a left-brain thinker without any concept of how difficult
 it is for a right-brain thinker to think like you. Care to compare
 poetry? I'd bury you.

That may be so, but the difference is that Steve is not trying to earn a
living writing poetry as far as I know, whereas you appear to be trying
to earn a living writing programs. If you can't learn to think like a
programmer, sooner or later you'll have to face the grim reality that
you'll never earn a living as a programmer. Maybe you should try to earn
a living as a poet instead.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 12:43 PM, Carsten Haese carsten.ha...@gmail.comwrote:

 Anyway, the likely answer is that you guessed incorrectly. As I said
 before, you need to make sure that the cookie is printed as part of the
 page headers. I'll give you one last hint: The page header is where
 you're printing the Content-type line.


Ok. Thank you.

  print 'Content-Type: text/html'
  print cookie
  print
  print '''!DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
html
'''

That worked. Threw a new error LOL.
Thanks,
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 12:51 PM, Carsten Haese carsten.ha...@gmail.comwrote:

 Victor Subervi wrote:
  Comments from a left-brain thinker without any concept of how difficult
  it is for a right-brain thinker to think like you. Care to compare
  poetry? I'd bury you.

 That may be so, but the difference is that Steve is not trying to earn a
 living writing poetry as far as I know, whereas you appear to be trying
 to earn a living writing programs. If you can't learn to think like a
 programmer, sooner or later you'll have to face the grim reality that
 you'll never earn a living as a programmer. Maybe you should try to earn
 a living as a poet instead.


EEK GADS! He can't even understand a metaphor! Can't you use your right
brain to understand a metaphor?! Do you have to take EVERYTHING literally?
Poor soul! You've lost it!

Revenge is sweet :)

LOL

(Disclaimer: The above statements were made with tongue planted firmly in
cheek. They are not intended to give offense.)
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
On Wed, Dec 30, 2009 at 12:51 PM, Carsten Haese carsten.ha...@gmail.comwrote:

 Victor Subervi wrote:
  Comments from a left-brain thinker without any concept of how difficult
  it is for a right-brain thinker to think like you. Care to compare
  poetry? I'd bury you.

 That may be so, but the difference is that Steve is not trying to earn a
 living writing poetry as far as I know, whereas you appear to be trying
 to earn a living writing programs. If you can't learn to think like a
 programmer, sooner or later you'll have to face the grim reality that
 you'll never earn a living as a programmer. Maybe you should try to earn
 a living as a poet instead.


PS. There is something I am led to explore in programming. I'm doing ok.
Once I'm on my feet (I just arrived back in the states from the Dominican
Republic where I went broke) I'll go back to outsourcing and just be the
front end for my business. I'm damn good in sales. But I have to build this
shopping cart first, and frankly, despite it all, I'm rather enjoying it.
Yeah, if you can find me a publisher who's interested in a ripping hot epic
poem, let me know. Haven't found one yet...wrong century. I'm working up my
singing to start hitting the clubs and make money that way. But Carsten, I
dont' work for money. I work for fun. And despite it all, I actually enjoy
this. So I will continue...until something I enjoy more, like maybe music
and dance, intervenes.
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Victor Subervi
You know, neither one of those tutorials I followed gave clear, or any,
instruction about putting a
print cookie
statement in the header! How misleading!
beno
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Steve Holden
Victor Subervi wrote:
 You know, neither one of those tutorials I followed gave clear, or any,
 instruction about putting a
 print cookie
 statement in the header! How misleading!
 beno
 
At the risk of repeating myself, if you understood more about what you
are trying to do you wouldn't have needed to be told because you would
have known that the cookies were transmitted as HTTP headers and that
you had to somehow put that information into the headers (in other
words, that creating a cookie was not sufficient to insert it into the
HTTP output stream).

regards
 Steve
-- 
Steve Holden   +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS:http://holdenweb.eventbrite.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-30 Thread Carsten Haese
Victor Subervi wrote:
 You know, neither one of those tutorials I followed gave clear, or any,
 instruction about putting a
 print cookie
 statement in the header! How misleading!

Don't blame the tutorials for your failure to read them carefully. The
tutorial you mentioned
(http://www.doughellmann.com/PyMOTW/Cookie/index.html) says the
following right at the beginning: The Cookie module defines classes for
parsing and creating HTTP cookie *headers*. [Emphasis mine.]

Further down it says: The output is a valid Set-Cookie *header* ready
to be passed to the client as part of the HTTP response. [Emphasis
mine.] You even copy-and-pasted a snippet containing that very sentence
into an earlier post on this thread, so how you can now claim that the
tutorial didn't mention this is quite beyond me.

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies

2009-12-29 Thread Carsten Haese
Victor Subervi wrote:
 Hi;
 I have these lines:
 
   cookie = os.environ.get('HTTP_COOKIE')
   if not cookie:
 cookie = Cookie.SimpleCookie()
 cExpires, cPath, cComment, cDomain, cMaxAge, cVersion = myCookie()
 cookie['lastvisit'] = str(time.time())
 cookie['lastvisit']['expires'] = cExpires
 cookie['lastvisit']['path'] = cPath
 cookie['lastvisit']['comment'] = cComment
 cookie['lastvisit']['domain'] = cDomain
 cookie['lastvisit']['max-age'] = cMaxAge
 cookie['lastvisit']['version'] = cVersion
 cookieFlag = 'new'
   else:
 cookie = Cookie.SimpleCookie(cookie)
 cookieFlag = 'old'
   print '''Content-Type: text/html\r\n
 !DOCTYPE html PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
 html
 '''
   print cookieFlag
 
 cookieFlag prints 'new'. Every time. Even when I refresh. I've imported
 Cookie. I've followed the tutorials :-} I've already been through my
 problem with trying to find the cookies on this computer. The fact that
 I'm using gmail is proof that cookies are enabled. I have also tried:
 cookie = os.environ.has_key('HTTP_COOKIE')
 Please advise.

You apparently haven't followed the tutorials carefully enough. You do
know that a cookie is a piece of information that's stored in your
browser, don't you? So tell me, which of the above lines of code do you
suppose is responsible for informing your browser of the cookie's contents?

--
Carsten Haese
http://informixdb.sourceforge.net

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies and CookieJar

2008-05-17 Thread 7stud
On May 16, 9:21 am, Larry Bates [EMAIL PROTECTED] wrote:
 I'm struggling with a project using mechanize and cookies to screen scape a
 website.  The site requires a client created cookie for authentication.  Below
 is the code I'm attempting to use with the traceback I'm getting:

   import Cookie
   c=Cookie.SimpleCookie()
   c[Manageopen]=cards
   c['Manageopen']['expires'] = 0
   c['Manageopen']['path'] = /
   c['Manageopen']['domain'] = .domain.com
   c['Manageopen']['secure'] = 
   c.output()
 'Set-Cookie: Manageopen=cards; Domain=.domain.com; expires=Fri, 16-May-2008
 14:06:00 GMT; Path=/'
   import cookielib
   cj=cookielib.CookieJar()
   cj.set_cookie(c)
 Traceback (most recent call last):
    File interactive input, line 1, in module
    File C:\Python25\lib\cookielib.py, line 1617, in set_cookie
      if cookie.domain not in c: c[cookie.domain] = {}
 AttributeError: 'SimpleCookie' object has no attribute 'domain'
  

 I also tried:

   cj.set_cookie(c[Manageopen])
 Traceback (most recent call last):
    File interactive input, line 1, in module
    File C:\Python25\lib\cookielib.py, line 1617, in set_cookie
      if cookie.domain not in c: c[cookie.domain] = {}
 AttributeError: 'Morsel' object has no attribute 'domain'

 I've looked at everything I can find via Google and nothing seems to help.

 Thanks in advance.

 Regards,
 Larry

Hi,

In the python docs, set_cookie() is defined like this:

---
set_cookie(cookie)
Set a Cookie, without checking with policy to see whether or not it
should be set.
---

The Cookie mentioned there is a an object of the Cookie class which is
defined in the cookielib module.  On the other hand,
Cookie.SimpleCookie() creates an object of a class called SimpleCookie
(and SimpleCookie inherits from a class called BaseCookie).
Therefore, the object returned by Cookie.SimpleCookie() is not a
Cookie--it's a SimpleCookie.  Just because the module that contains
the SimpleCookie class is called Cookie does not mean that
SimpleCookie inherits from the Cookie class or that SimpleCookie has
any connection to the Cookie class located in the cookielib module.
If that's too confusing, here is the way the python modules are set
up:

Cookie module:
-
BaseCookie class
|
V
SimpleCookie class




cookielib module

Cookie class

CookieJar class
---set_cookie(Cookie)


What you did in your code was you supplied set_cookie() with a
SimpleCookie object, and set_cookie() expects a Cookie object.
SimpleCookie objects are a mapping and you access the data using the
notation:

   c['Manageopen']['domain'] = .domain.com

But if you open up cookilib.py and look at the line in the error
message:

 File C:\Python25\lib\cookielib.py, line 1617, in set_cookie

you will see that the code uses the notation:

      if cookie.domain ...

That's because the Cookie class defines simple objects that have
attributes that are accessed normally, i.e. with dot notation.


 The site requires a client created cookie for authentication.

A cookie is just one of the headers in a request.  Instead of using
a url string to retrieve a web page with urllib2.urlopen(), you can
use a Request object.  And Request objects allow you to set headers,
which means you can set a cookie header.

But the site will probably check every subsequent request for that
cookie header as well, so that means you will need to add the header
to every subsequent request you make.  That is where CookieJar can
help you.  It can store cookies in a file and then automatically add
them to every request for you.  It's a bit complicated though.  See
here:

http://www.voidspace.org.uk/python/articles/cookielib.shtml










--
http://mail.python.org/mailman/listinfo/python-list


Re: Cookies and CookieJar

2008-05-17 Thread 7stud
Larry Bates wrote:
 I'm struggling with a project using mechanize and cookies to screen scape a
 website.  The site requires a client created cookie for authentication.

Are you sure that is correct?  As far as I know, the usual course of
events is for a site to set a cookie header in the response it sends
back to the client browser, which gets stored on the client's
computer.  Then when the browser makes subsequent requests to that
site, the browser will automatically include the cookies that were set
by that site.
--
http://mail.python.org/mailman/listinfo/python-list