我正在使用Python的Requests库进行HTTP请求。我从服务器获取到的cookie是文本形式的。如何将其转换为包含该cookie的CookieJar
对象?
我正在使用Python的Requests库进行HTTP请求。我从服务器获取到的cookie是文本形式的。如何将其转换为包含该cookie的CookieJar
对象?
Requests库的旧版本(0.14.2及更早版本)在您传递一个CookieJar
对象时,会自动将新的cookie添加到cookie jar中:
import requests
import cookielib
URL = '...whatever...'
jar = cookielib.CookieJar()
r = requests.get(URL, cookies=jar)
r = requests.get(URL, cookies=jar)
第一次请求URL时将填充cookie jar,第二次请求会将cookie发送回服务器。
这在Requests 1.0.0(于2012年发布)之后的版本中不再适用。
requests.Session
对象,则不需要这样做;它将完全处理cookie。 - Martijn Pietersrequests.Session
可以完美地运行。 - djsumdog一个 Requests Session
可以接收和发送 cookies。
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"}}'
上面的代码是从 会话对象 中偷来的。
如果您想要使 cookie 在代码运行之间持久化保存在磁盘上,可以直接使用 CookieJar
并进行保存和加载:
from http.cookiejar import LWPCookieJar
import requests
cookie_file = '/tmp/cookies'
jar = LWPCookieJar(cookie_file)
# Load existing cookies (file might not yet exist)
try:
jar.load()
except:
pass
s = requests.Session()
s.cookies = jar
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get("http://httpbin.org/cookies")
# Save cookies to disk, even session cookies
jar.save(ignore_discard=True)
然后查看文件 /tmp/cookies:
#LWP-Cookies-2.0
Set-Cookie3: sessioncookie=123456789; path="/"; domain="httpbin.org"; path_spec; discard; version=0
ignore_discard=True
的提示!我之前找不出为什么我的 JAR 文件保存了一个空文件。 - Michael我认为许多答案都没有抓住重点。有时候,那个库不是在使用Requests。或者它不会暴露它正在使用的cookiejar。有时候我们所拥有的只是cookie字符串。在我的情况下,我正在尝试借用来自pyVmomi的auth cookie。
import requests
import http.cookies
raw_cookie_line = 'foo="a secret value"; Path=/; HttpOnly; Secure; '
simple_cookie = http.cookies.SimpleCookie(raw_cookie_line)
cookie_jar = requests.cookies.RequestsCookieJar()
cookie_jar.update(simple_cookie)
这给了我们以下的cookie_jar
:
In [5]: cookie_jar
Out[5]: <RequestsCookieJar[Cookie(version=0, name='foo', value='a secret value', port=None, port_specified=False, domain='', domain_specified=False, domain_initial_dot=False, path='/', path_specified=True, secure=True, expires=None, discard=False, comment='', comment_url=False, rest={'HttpOnly': True}, rfc2109=False)]>
我们可以像平常一样使用它:
requests.get(..., cookies=cookie_jar)
import Cookie,cookielib,requests,datetime,time # I had this out, but I realized later I needed it when I continued testing
def time_to_tuple(time_string):
wday = {'Mon':0,'Tue':1,'Wed':2,'Thu':3,'Fri':4,'Sat':5,'Sun':6}
mon = {'Jan':1,'Feb':2,'Mar':3,'Apr':4,'May':5,'Jun':6,'Jul':7,'Aug':8,'Sep':9,'Oct':10,'Nov':11,'Dec':12}
info = time_string.split(' ')
info = [i.strip() for i in info if type(i)==str]
month = None
for i in info:
if '-' in i:
tmp = i.split('-')
for m in tmp:
try:
tmp2 = int(m)
if tmp2<31:
mday = tmp2
elif tmp2 > 2000:
year = tmp2
except:
for key in mon:
if m.lower() in key.lower():
month = mon[key]
elif ':' in i:
tmp = i.split(':')
if len(tmp)==2:
hour = int(tmp[0])
minute = int(tmp[1])
if len(tmp)==3:
hour = int(tmp[0])
minute = int(tmp[1])
second = int(tmp[2])
else:
for item in wday:
if ((i.lower() in item.lower()) or (item.lower() in i.lower())):
day = wday[item]
if month is None:
for item in mon:
if ((i.lower() in item.lower()) or (item.lower() in i.lower())):
month = mon[item]
return year,month,mday,hour,minute,second
def timefrom(year,month,mday,hour,minute,second):
time_now = time.gmtime()
datetime_now = datetime.datetime(time_now.tm_year,time_now.tm_mon,
time_now.tm_mday,time_now.tm_hour,
time_now.tm_min,time_now.tm_sec)
then = datetime.datetime(year,month,mday,hour,minute,second)
return (datetime_now-then).total_seconds()
def timeto(year,month,mday,hour,minute,second):
return -1*timefrom(year,month,mday,hour,minute,second)
##['comment', 'domain', 'secure', 'expires', 'max-age', 'version', 'path', 'httponly']
def parse_request(request):
headers = request.headers
cookieinfo = headers['set-cookie'].split(';')
name = 'Undefined'
port=80
port_specified=True
c = Cookie.SmartCookie(headers['set-cookie'])
cj = cookielib.CookieJar()
for m in c.values():
value = m.coded_value
domain = m['domain']
expires = m['expires']
if type(expires) == str:
tmp = time_to_tuple(expires)
expires = timeto(tmp[0],tmp[1],tmp[2],tmp[3],tmp[4],tmp[5])
max_age=m['max-age']
version = m['version']
if version == '':
version = 0
path = m['path']
httponly = m['httponly']
if httponly == '':
if 'httponly' in headers['set-cookie'].lower():
httponly = True
else:
httponly = False
secure = m['secure']
comment=m['comment']
port = 80
port_specified=False
domain_specified=True
domain_initial_dot = domain.startswith('.')
path_specified=True
discard = True
comment_url=None
rest={'HttpOnly':httponly}
rfc2109=False
ck = cookielib.Cookie(version,name,value,port,port_specified,domain,
domain_specified,domain_initial_dot,path,path_specified,
secure,expires,discard,comment,comment_url,rest,rfc2109)
cj.set_cookie(ck)
return cj
cookielib.CookieJar.extract_cookies(response, request)
吗? - MestreLion好的,cookielib.LWPCookieJar类有load和save方法。看一下格式并检查是否与本机cookie格式匹配。您可能可以使用StringIO将Cookie直接加载到CookieJar中。
或者,如果Requests在后台使用urllib2,则可以向默认打开程序添加一个cookie处理程序。
我正在尝试做同样的事情。目前为止,我已经做了一些工作,但出于某种原因,它没有在头部发送cookie。不过这可能已经足够让你解决问题了。
import requests
import cookielib
import logging
log = logging.getLogger(__name__)
def auth(auth_url, cookies):
cj = cookielib.CookieJar()
for x in cookies:
if len(cookies[x]) > 0:
ck = cookielib.Cookie(version=1, name=x, value=cookies[x],
port=None, port_specified=False, domain='.example.com',
domain_specified=True,
domain_initial_dot=True, path='/',
path_specified=True, secure=False,
expires=None, discard=True,
comment=None, comment_url=None,
rest=None, rfc2109=True)
log.info(ck)
cj.set_cookie(ck)
log.info("cookies = %s " % cj)
response = requests.get(auth_url, cookies=cj)
log.info("response %s \n" % response)
log.info("response.headers %s \n" % response.headers)
log.info("response.content %s \n" % response.content)
我假设你已经请求了url
,并且你得到了headers
作为响应。 url
的类型是字符串。 headers
的类型是列表。
import urllib2
import cookielib
class dummyResponse:
def __init__(self, headers):
self.headers = headers
def info(self):
return dummyInfo(self.headers)
class dummyInfo:
def __init__(self, headers):
self.headers = headers
def getheaders(self, key):
# Headers are in the form: 'Set-Cookie: key=val\r\n'. We want 'key=val'
newMatches = []
for header in self.headers:
if header.lower().startswith(key.lower()):
clearHeader = header[len(key) + 1:].strip()
newMatches.append(clearHeader)
return newMatches
req = urllib2.Request(url)
resp = dummyResponse(headers)
jar = cookielib.CookieJar()
jar.extract_cookies(resp, req)
以下是一个简化版的overthink's answer,介绍如何在Python 3中获取cookiejar并持久化cookies:
import requests
s = requests.Session()
r1 = s.get('https://stackoverflow.com')
print("r1", r1.cookies) # Have cookie
print("s", s.cookies) # Have cookie(jar)
r2 = s.get('https://stackoverflow.com') #The cookie from r1 is resend
print("r2", r2.cookies) #No cookie (could be a new one)
print("s", s.cookies) #Keep the cookie(jar) from r1
正如dstanek所回答的那样,Requests会自动将响应cookie放入cookie jar中。
然而,如果您手动指定了Cookie
头条目,Requests将不会为您将这些cookie放入jar中。这意味着任何后续请求都将缺少您最初设置的cookie,但它将拥有任何新的cookie。
如果您确实需要手动为请求创建cookie jar,请使用requests.cookies.RequestsCookieJar
。以防他们的示例代码发生更改:
jar = requests.cookies.RequestsCookieJar()
jar.set('tasty_cookie', 'yum', domain='httpbin.org', path='/cookies')
jar.set('gross_cookie', 'blech', domain='httpbin.org', path='/elsewhere')
url = 'http://httpbin.org/cookies'
r = requests.get(url, cookies=jar)
requests.Session()
,您无需担心cookie jars的问题。会话对象会为您管理接收和发送cookies。 - Martijn Pieters