Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try using <a href="http://docs.python.org/library/httplib.html#httplib.HTTPResponse.getheaders" rel="nofollow"><code>getheaders()</code></a> to get a <code>list</code> of the cookies:</p> <pre><code>&gt;&gt;&gt; msg = resp.info() &gt;&gt;&gt; msg.getheaders('Set-Cookie') ['PREF=ID=5975a5ee255f0949:FF=0:TM=1305336283:LM=1305336283:S=1vkES6eF4Yxd-_oM; expires=Mon, 13-May-2013 01:24:43 GMT; path=/; domain=.google.com.au', 'NID=46=lQVFZg6yKUsoWT529Hqp5gA8B_CKYd2epPIbANmw_J0UzeMt2BhuMF-gtmGsRhenUTeajKz2zILXd9xWpHWT8ZGvDcmNdkzaGX-L_-sKyY1w4e2l3DKd80JzSkt2Vp-H; expires=Sun, 13-Nov-2011 01:24:43 GMT; path=/; domain=.google.com.au; HttpOnly'] </code></pre> <p>In this case, you get a <code>list</code> of two <code>string</code>s.</p> <p>Then you can iterate over that <code>list</code> and grab whichever cookie you like. <a href="http://docs.python.org/library/stdtypes.html#str.startswith" rel="nofollow"><code>str.startswith()</code></a> is your friend:</p> <pre><code>&gt;&gt;&gt; cookies = msg.getheaders('Set-Cookie') &gt;&gt;&gt; for cookie in cookies: ... if cookie.startswith('PREF='): ... print 'Got PREF: ', cookie ... else: ... print 'Got another: ', cookie ... Got PREF: PREF=ID=5975a5ee255f0949:FF=0:TM=1305336283:LM=1305336283:S=1vkES6eF4Yxd-_oM; expires=Mon, 13-May-2013 01:24:43 GMT; path=/; domain=.google.com.au Got another: NID=46=lQVFZg6yKUsoWT529Hqp5gA8B_CKYd2epPIbANmw_J0UzeMt2BhuMF-gtmGsRhenUTeajKz2zILXd9xWpHWT8ZGvDcmNdkzaGX-L_-sKyY1w4e2l3DKd80JzSkt2Vp-H; expires=Sun, 13-Nov-2011 01:24:43 GMT; path=/; domain=.google.com.au; HttpOnly </code></pre> <h2>How a newbie can find the documentation in Python</h2> <pre><code>% python Python 2.7.1 (r271:86832, Jan 29 2011, 13:30:16) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information. &gt;&gt;&gt; import urllib2 &gt;&gt;&gt; req = urllib2.Request('http://www.google.com') &gt;&gt;&gt; resp = urllib2.urlopen(req) &gt;&gt;&gt; help(resp.info()) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload