Cookies not showing up in environ

2018-07-20 Thread abc abc
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


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-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 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-25 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