Using python library requests to connect a Django application failed authentication

Maybe a stupid question: Requests (Python HTTP lib) support Django 1.4?

I use Queries following the official quick launch as shown below:

requests.get('http://127.0.0.1:8000/getAllTracks', auth=('myUser', 'myPass'))

but i never get authentication. (Of course, I checked the URL, username, password again and again.)

The above URL ' http://127.0.0.1:8000/getAllTracks ' matches the url.py template of the Django project, and this url template callback is getAllTracks ' of the Django application.

If I comment on the authentication code " getAllTracks ", then the above code works fine, but if I add this authentication code for the submission, then the specified code will never be authenticated to the right.

The authentication code is actually very simple, as shown below (second line):

def getAllTracks(request):
    if request.user.is_authenticated():
        tracks = Tracks.objects.all()
        if tracks:
            # Do sth. here

This means that if I delete the second line above (with some indentation adjustments, of course), then the request.get () operation does what it needs for me, but if not (keep the second line) then it will never be right .

Any help would be appreciated.

+3
source share
3 answers

In Django, authentication works as follows:

  • SessionMiddleware AuthenticationMiddleware. _request() .
  • SessionMiddleware cookie . cookie sessionid cookie .
  • AuthenticationMiddleware , cookie , request.user . cookie sessionid - , request.user AnonymousUser().
  • Http , django , cookie .

, requests django.

, . sessionid cookie.

cookie , django request.user.is_authenticated() .

from django.contrib.auth import authenticate, login

def login_user(request):
    user = authenticate(username=request.POST.get('username'),  password=request.POST.get('password'))
    if user:
       login(request, user)
       return HttpResponse("Logged In")
    return HttpResponse("Not Logged In")

def getAllTracks(request):
    if request.user.is_authenticated():
        return HttpResponse("Authenticated user")
    return HttpResponse("Non Authenticated user")

:

import requests

resp = requests.post('http://127.0.0.1:8000/login/', {'username': 'akshar', 'password': 'abc'})

print resp.status_code
200 #output

print resp.content
'Logged In' #output

cookies = dict(sessionid=resp.cookies.get('sessionid'))

print cookies
{'sessionid': '1fe38ea7b22b4d4f8d1b391e1ea816c0'}  #output

response_two = requests.get('http://127.0.0.1:8000/getAllTracks/', cookies=cookies)

, cookie cookies keyword

print response_two.status_code
200  #output 

print response_two.content
'Authenticated user'  #output

, request.user.is_authenticated() .

response_three = requests.get('http://127.0.0.1:8000/hogwarts/getAllTracks/')

, cookie .

print response_three.content
'Non Authenticated user' #output
+12

, auth HTTP Basic authentication, , Django. POST URL- , POST, .

+3

cookie , , , , :

r = requests.post('http://127.0.0.1:8000', auth=(UN, PW))
self.token = r.cookies['token']
self.headers = {'token': token}

, , :

r = requests.post('http://127.0.0.1:8000/getAllTracks', headers=self.headers)
+1

All Articles