![](/img/trans.png)
[英]Need to requests.get a cookie (ASP.NET_SessionID) and then requests.post, the cookie as a parameter
[英]How to send a cookie with requests.get or requests.post in python?
我很難將以下bash腳本轉換為python
以下作品:
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 ]; then
echo $COOKIE_FILE
curl -b $COOKIE_FILE http://localhost:8080/data
fi
rm -f $COOKIE_FILE
現在,在Python中,我不確定如何完成cookie部分
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:
# it reaches here fine
cookies = ????
response = requests.get(url='http://localhost:8080/data',
cookies=?????)
print response.status_code
print response.text
您也可以使用Python進行首次通話。 這將容易得多,並利用python的優勢。 尚未測試。
#!/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:
data = session.get(url='http://localhost:8080/data')
print data, data.text
如果您希望cookie繼續存在,請查看此處 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.