I've written a script on my local machine to log into my PythonAnywhere instance and attempt to get my access log files. To do this, I first login to the site like this:
session_requests = requests.session()
result = session_requests.get(BASE_URL)
token = result.cookies['csrftoken']
payload = {'auth-username': 'pjones',
'auth-password': '[my pwd]',
'csrfmiddlewaretoken': token}
result = session_requests.post(BASE_URL, data=payload, headers=dict(referer=BASE_URL))
Here BASE_URL
is just https://www.pythonanywhere.com. This works fine in that it comes back with a 200 status code and the HTML for a page that is something other than the login page. But when immediately after I do this:
url = 'https://www.pythonanywhere.com/user/pjones/'
page = session_requests.get(url, headers=dict(referer=url))
print(page.status_code)
print(page.text)
I get another 200 response, but the text content of it is the HTML for the login page rather than my dashboard page as I'd expect. It's almost as if the login has a (very short) lifetime but I can't find anything in the requests
documentation indicating that it implements such a thing.
Is there something specific to PythonAnywhere that is making this happen? Thanks!