I'm using this code to login to an experiment login system created by me for this purpose.
import requests
import re
def get_page_data(regex, req):
match = re.compile(regex).search(req.text)
if match != None:
return match.group(1)
return 'no match found on {}'.format(regex)
def print_req_data(req, req_name):
print('{} status code: {}'.format(req_name, req.status_code))
print('{} title: {}'.format(req_name, get_page_data('<title>(.*?)</title>', req)))
print('{} content:\n{}'.format(req_name, req.text))
form_url = 'http://migueldvl.com/heya/login/'
post_url = 'http://migueldvl.com/heya/login/process.php'
headers = {'User-agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1'}
with requests.Session() as s:
# get form page, so the cookies are set (form token)
logged_out_req = s.get(form_url, headers=headers)
print_req_data(logged_out_req, 'Logged out request')
title_logout = get_page_data('<title>(.*?)</title>', logged_out_req)
# getting the form token
token = get_page_data('<input type="hidden" name="token" value="(.*?)">', logged_out_req)
login_data = {'password' : 'password', 'username' : 'miguel', 'token': token}
# posting the data to the post url
post_req = s.post(post_url, data=login_data, headers=headers)
print_req_data(post_req, 'Post request (redirect page)')
check_post_title = get_page_data('<title>(.*?)</title>', post_req)
# comparing the titles (logged out title with redirect page title) so i see if login success
if(check_post_title != title_logout):
print('SUCCESS\n[+] Checking if still logged in...')
# going to see if i'm still loggin, see if our loggedin session is permanent
logged_req = s.get(form_url, headers=headers)
print_req_data(logged_req, 'Check if still logged request')
title_check = get_page_data('<title>(.*?)</title>', logged_req)
if(title_check == check_post_title):
print('You are still loogedin')
else:
print('Not loggeding anymore')
else:
print('FAIL LOGIN')
print_req_data(post_req, 'Post request (redirect page)')
I was wondering if there is a better way to do this, especially with regard to:
Is there a better way to get the form token? I tried checking the headers,
logged_out_page = s.get(form_url, headers=headers) print(logged_out_page.headers) # headers the server sent back to us print(logged_out_page.request.headers) # headers we sent to the server
I know that session values are not stored on our machine (browser) unlike normal cookies, those are stored in the server so the real session value is not on the headers (just some id key to the server, so it can fetch the real data stored on it). But is there anyway besides using regex to extract the session value? PS: (session cookie produced with php, $_SESSION['token] = md5(time())
).
- Is there a way (besides doing a string comparison of the page title, or some other element on the page) to check if the login was successful?
Any other comments and suggestions are, of course, welcome. If you'd like to test this code (python3, using the links above, form_url
and post_url
) with the username miguel
and the password password
.
PS: Bear in mind that this code is based on the loggedin system mechanic that I created (the most usual, i guess). I don't wish any improvements on this code based on another