Guest User

login with requests (python)

a guest
Aug 30th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.42 KB | None | 0 0
  1. import requests
  2. import time
  3.  
  4. with requests.Session() as c:
  5.     #Define my varibles
  6.     url = 'http://itsupport.work.com.au/scp/login.php'
  7.     userName = 'my.name'
  8.     passWord = 'Pa$$word123'
  9.     do = 'scplogin'
  10.     submit = " "
  11.  
  12.     #Navigate to page to get token/cookie (Cookie's name = OSTSESSID  , csrftoken name = __CSRFToken__ )
  13.     #If I use this cookie name, the login fails AND if I use __CSRFToken__ as the cookie name I get an error
  14.     #However I think I'm suppose to be using this cookie name?
  15.     c.get(url)
  16.     time.sleep(3)
  17.     csrftoken = c.cookies['OSTSESSID']
  18.  
  19.     #Debug line
  20.     print(csrftoken)
  21.  
  22.     #Form data that I'm sending to the login.php page (note: I've put it in the exact order the data is being sent)
  23.     login_data = {"__CSRFToken__": csrftoken, 'do': do, 'userid': userName, 'passwd': passWord "submit": sumbit,}
  24.  
  25.     #Post request to login url, with form data and header/s (Could this be where the issue lies?)
  26.     c.post(url, data = login_data, headers = {'Referer': 'http://itsupport.work.com.au/scp/login.php', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'})
  27.  
  28.     #Testing if I've logged into the site, however I only get back the contents of the login page; meaning I didn't successfully login
  29.     page = c.get('http://itsupport.work.com.au/scp/dashboard.php')
  30.     print(page.content)
  31.     time.sleep(40)
Add Comment
Please, Sign In to add comment