so i'm new python , requests , i'm trying login site python requests , seems work alright @ first, use session , move page keep getting redirected login page (in python script).
i noticed similar issue when testing site ie 11, site works fine chrome or ff. pulled developer tools , compared on ie 11 , chrom cookies different:
ie 11: cookie: __qca=p0-193635655-1454037632726; phpsessid=f4hmvt7cvfrfqg0seivmu34fv7
chrome:cookie:__qca=p0-701638259-1453424539435; smfcookie474=a%3a4%3a%7bi%3a0%3bs%3a5%3a%2299842%22%3bi%3a1%3bs%3a40%3a%22dc7db4be19f7f4fc53d2411065b8b7bf5705c83a%22%3bi%3a2%3bi%3a1454036767%3bi%3a3%3bi%3a0%3b%7d; phpsessid=o43kja5a9iq1b8ibdll5sh2s24
python requests: cookie phpsessid=hotj0fpm5pbipktmm51kirul45
so assume i'm missing cookies python requests make work. advice?
def login_test(): payload = { 'user': 'user', 'passwrd': 'pass' } requests.session() s: p = s.post('http://www.example.com/smf/index.php?action=login2', data=payload) # print html returned or more intelligent see if it's successful login page. f = open("ouput.html", 'wb') print (s.cookies) #f.write(p.text.encode('utf8')) print ("status code: ", p.status_code) payload = { 'test': 'testparm' } r = s.get('http://www.example.com/cgi-bin/vote_rank.cgi', data=payload) print (r.status_code) print (s.cookies) print (r.headers) f.write(r.text.encode('utf8')) f.close() login_test()
i able fix adding in cookies manually:
def login_test(): payload = { 'user': 'user', 'passwrd': 'pass' } s = requests.session() p = s.post('http://www.example.com/smf/index.php?action=login2', payload, allow_redirects=false) # print html returned or more intelligent see if it's successful login page. #f = open("ouput.html", 'wb') cookies = p.headers['set-cookie'] n_cookies = {} m = re.compile('(\w+)=([a-z%0-9a-z]+)[^;]*') (name, content) in re.findall(m, cookies): if name != "expires": n_cookies[name] = content r = s.get('http://www.example.com/cgi-bin/vote_rank.cgi?mud=test', cookies = n_cookies)
if has better answer please add.
Comments
Post a Comment