Z
Z
zlodiak2014-02-24 13:01:51
Python
zlodiak, 2014-02-24 13:01:51

How to automatically use the session when authorizing through a script?

Please help me figure out the authorization through the script. The problem is that it is not possible to automatically insert invbf_session_id into the get request.

import pprint
import requests
import re
import shelve

import bs4
     

def scanning_posts():
    print('------------------------enter begin--------------------')

    url = 'http://forum.saransk.ru/'
    html = requests.get(url)

    pprint.pprint(html.headers)
    rawCookie = html.headers['Set-Cookie']
    cookie = re.search(r"invbf_session_id=(.*?);", rawCookie).group(1)
    pprint.pprint(cookie)	# cookie != zzzzzzzzzzzzzzzzzzzzzz

    html = requests.get(url)
    soup = bs4.BeautifulSoup(html.text)
    loginForm = soup.find('form', {'id': 'login'})
    hiddenAuthKey = soup.find('input', {'name': 'auth_key'})['value']

    authData = {    
                'ips_username': 'xxxxxx',
                'ips_password': 'yyyyyy',
                'auth_key': hiddenAuthKey,
                'rememberMe': 1,
                'referer': 	'http://forum.saransk.ru/'
    }

    header = {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.107 Safari/537.36',
                'Referer': 'http://forum.saransk.ru/forum/'
    }

    #pprint.pprint(authData)
    print('\tlogin: ', authData['ips_username'])

    cookie = dict(invbf_session_id='zzzzzzzzzzzzzzzzzzzzzz')



    req = requests.get(url, params=authData, cookies=cookie, headers=header)
    soup = bs4.BeautifulSoup(req.text)

    signLinkNotLogged = soup.find('a', {'id': 'sign_in'})
    if signLinkNotLogged:
        print('------------------------enter failed--------------------')
    else:
        print('------------------------enter successful--------------------')
    
    
scanning_posts()

After running the script, it displays a different value than what is seen in FF firebug. As a result, login fails.
If the value of invbf_session_id is copied from FF firebug and pasted into the script, then authorization is successful

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question