How do I authenticate a urllib2 script in order to access HTTPS web services from a Django site?

前端 未结 2 1401
天命终不由人
天命终不由人 2021-01-03 09:18

everybody. I\'m working on a django/mod_wsgi/apache2 website that serves sensitive information using https for all requests and responses. All views are written to redirect

2条回答
  •  时光说笑
    2021-01-03 09:53

    Please excuse my answering my own question, but - for the record this seems to have solved it:

    It turns out I needed to set the HTTP Referer header to the login page url in the request where I post the login information.

    req.add_header( 'Referer', login_url )
    

    The reason is explained on the Django CSRF documentation - specifically, step 4.

    Due to our somewhat peculiar server setup where we use HTTPS on the production side and DEBUG=False, I wasn't seeing the csrf_failure reason for failure (in this case: 'Referer checking failed - no referer') that is normally output in the DEBUG info. I ended up printing that failure reason to the Apache error_log and STFW'd on it. That lead me to code.djangoproject/.../csrf.py and the Referer header fix.

提交回复
热议问题