How to send a cookie with requests.get or requests.post in python? -



How to send a cookie with requests.get or requests.post in python? -

i'm having hard time converting next bash script python

the next works:

username=username password=password cookie_file=app_cookies echo $cookie_file res=`curl -s -l -c $cookie_file -b $cookie_file -d "j_username=$username&j_password=$password" http://localhost:8080/j_security_check | grep "authenticated" | wc -l` if [ $res -gt 0 ]; echo $cookie_file curl -b $cookie_file http://localhost:8080/data fi rm -f $cookie_file

now in python, i'm not sure how finish cookies part

cookie_file="app_cookies" username=username password=password result = os.system("curl -s -l -c " + cookie_file + " -b " + cookie_file + " -d \"j_username=" + username + "&j_password=" + password + "\" http://localhost:8080/j_security_check | grep \"authenticated\" | wc -l") # authenticated if result == 0: # reaches here fine cookies = ???? response = requests.get(url='http://localhost:8080/data', cookies=?????) print response.status_code print response.text

you can utilize python first call. much easier , take advantages of python. it's not tested.

#!/usr/bin/env python # -*- coding: utf-8 -*- import requests session = requests.session() payload = {'j_username': username, 'j_password': password} r = session.post(url='http://localhost:8080/j_security_check', data=payload) if u"authenticated" in r.text: info = session.get(url='http://localhost:8080/data') print data, data.text

if want cookie persist here.

python python-requests

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -