Wednesday 15 July 2015

Python + cookies + requests --- Can not open a link -



Python + cookies + requests --- Can not open a link -

i need log website using python login page requires sessionid cookie in request header. using google developer tools along webclient(hurl.it), able determine required format of request header acceptable webserver:

accept: */* accept-encoding: gzip, deflate content-length: 85 content-type: application/x-www-form-urlencoded cookie: www_amsterdam-dance-event_nl_session=l9abno8a1uyhpof%2boyvqk8bxhjesgmi78z6ot0zxccbi%2bxvkqjm30altfw%2fr7ykcdaqfetfoyystrjieu8lu5ylv1tolw6glhy8jdfkkwsulksuujith92dbvkuybue6zt%2fels44ldna6nz3umcoasarn7gcposz0tocfaes8hk9q6fikp1f9e%2b%2fsmwfup0rta0rc5gjfyjpxhxncdn%2bt49mhhynzoiwvlxghhlaekzx1ppsyx1xq0bcgpb0wnpviuizibnqy2nz%2bbo4uur0wpnfpsswzg5qxz79nyechlre16jhyjvodaiuhnfevp1jm7h%2bcdr6cueatd7hgbftrcjindrvupeyb5ltvihstmzkejomweti0xnuanswspikkuo%2bv6jfnfdlca6h3iy1k8o%2fa49tkgmp2rmge4e5jec%3df395212364d1ffc80cf95ebf5abf3b40f9dc6441; user-agent: runscope/0.1 email=******%40beatswitch.com&login_token=545a46230b291&password=*****&submission=

i have produced next request using python requests module:

post /my-ade/login/ http/1.1 host: www.amsterdam-dance-event.nl content-length: 85 accept-encoding: gzip,deflate accept: */* user-agent: runscope/0.1 connection: keep-alive cookie: www_amsterdam-dance-event_nl_session=l9abno8a1uyhpof%2boyvqk8bxhjesgmi78z6ot0zxccbi%2bxvkqjm30altfw%2fr7ykcdaqfetfoyystrjieu8lu5ylv1tolw6glhy8jdfkkwsulksuujith92dbvkuybue6zt%2fels44ldna6nz3umcoasarn7gcposz0tocfaes8hk9q6fikp1f9e%2b%2fsmwfup0rta0rc5gjfyjpxhxncdn%2bt49mhhynzoiwvlxghhlaekzx1ppsyx1xq0bcgpb0wnpviuizibnqy2nz%2bbo4uur0wpnfpsswzg5qxz79nyechlre16jhyjvodaiuhnfevp1jm7h%2bcdr6cueatd7hgbftrcjindrvupeyb5ltvihstmzkejomweti0xnuanswspikkuo%2bv6jfnfdlca6h3iy1k8o%2fa49tkgmp2rmge4e5jec%3df395212364d1ffc80cf95ebf5abf3b40f9dc6441; content-type: application/x-www-form-urlencoded login_token=545a46230b291&password=*****&email=******%40beatswitch.com&submission='

when load former request header hurl.it, works , webserver lets me log in trying almost-same request same parameters fails in python. while using python's request, webserver presents error page. help highly appreciated. need solution desperately.

edit: here code:

#open login page sessionid , login_token loginurl = "https://www.amsterdam-dance-event.nl/my-ade/login/" loginreq = session.get(loginurl) loginsoup = beautifulsoup(loginreq.text) logintoken = loginsoup.find('input',attrs={'name':'login_token'})['value'] sessionid= loginreq.cookies['www_amsterdam-dance-event_nl_session'] cookie = 'www_amsterdam-dance-event_nl_session='+sessionid #construct header , post webserver headers = {'content-length':'85','accept':'*/*','user-agent':' runscope/0.1','content-type':'application/x-www-form-urlencoded','accept-encoding':'gzip,deflate','cookie':cookie} payload = {'email':'*******@beatswitch.com','password':'********','login_token':logintoken,'submission':''} loggedinreq = session.post(loginurl,headers=headers,data=payload)

i found solution, md. mohsin. trying handle request headers , cookies manually while requests module can handle them itself. removed next line code , allow requests take total control, , worked intended:

headers = {'content-length':'85','accept':'*/*','user-agent':' runscope/0.1','content-type':'application/x-www-form-urlencoded','accept-encoding':'gzip,deflate','cookie':cookie}

python python-requests

No comments:

Post a Comment