Reputation: 557
I have this Python 2 code :
import requests
import json
import Cookie
USERNAME = "<user_name>"
PASSWORD = "<password>"
BACKEND = "https://blahblah.com"
# login will take orgId, email and password and return auth cookie for the user
def login(email, password):
url = 'https://auth-v2.blahblah.com/auth/v1.0/login'
data = json.dumps({'email':email, 'password': password})
r = requests.post(url,data=data,allow_redirects=False)
cookieHeaders = filter(lambda x: x.lower()=='set-cookie',r.headers.keys())
if len(cookieHeaders)==0:
return None
bc = Cookie.BaseCookie(r.headers[cookieHeaders[0]])
sess = 'sess='+bc.values()[0].value
return sess
But I can't translate it properly into Python 3.
I just need the Set-Cookie value from the headers, but either the filter function or lambda isn't working the same as in Python 2.
It looks like the filter() function in Python 2 returns a filter object which then fails the len() check and the "bc = " line as well.
How can I access the cookie values properly ?
Upvotes: 0
Views: 2230
Reputation: 1529
You may try build-in cookie feature:
import requests
def login(email, password):
url = 'https://wordpress.com/'
data = {'email': email, 'password': password}
r = requests.post(url, json=data, allow_redirects=False)
print(r.cookies)
print(r.cookies.get_dict())
print(r.cookies.get_dict(domain='wordpress.com'))
sess = 'sess={}'.format(r.cookies.get_dict()['wp_locale_test_group'])
print(sess)
return sess
will produce:
print(r.cookies)
→
<RequestsCookieJar[<Cookie SSE_blogger_ecommerce_20181217=test-a for .wordpress.com/>, <Cookie tk_ai=ZCGvpAX2VOVR5RO11DPzA1dm for .wordpress.com/>, <Cookie wp_locale_test_group=jan-2019 for wordpress.com/>]>
═══
print(r.cookies.get_dict())
→
{'SSE_blogger_ecommerce_20181217': 'test-a', 'tk_ai': 'ZCGvpAX2VOVR5RO11DPzA1dm', 'wp_locale_test_group': 'jan-2019'}
═══
print(r.cookies.get_dict(domain='wordpress.com'))
→
{'wp_locale_test_group': 'jan-2019'}
═══
print(sess)
→
sess=jan-2019
Upvotes: 0
Reputation: 180
Try using the Requests module and the requests.Session object Quick Start; Sessions
Refer this stackoverflow question for more detail
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')
print(r.text)
# '{"cookies": {"sessioncookie": "123456789"}}'
print(s.cookies)
# RequestsCookieJar[Cookie(version=0, name='sessioncookie', value='123456789', port=None, port_specified=False, domain='httpbin.org', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={}, rfc2109=False)]
Upvotes: 1
Reputation: 8298
The behavior is slightly different in Python 2 than in Python3.
In Python 2 the following works just fine:
foo = [1,2,3,4,5]
len(filter(lambda x: x>2, foo))
This works because type(filter(lambda x: x>2, foo))
is list
.
While in Python 3 you need a list
constructor:
foo = [1,2,3,4,5]
len(list(filter(lambda x: x>2, foo)))
Indeed in Python 3 type(filter(lambda x: x>2, foo))
is <class 'filter'>
.
Upvotes: 0