How to set and retrieve cookie in HTTP header in Python?

前端 未结 4 1831
孤独总比滥情好
孤独总比滥情好 2020-12-07 23:34

I need to get the cookies from a HTTP response sent by a server and put it in the next request\'s header. How can I do it?

Thanks in advance.

相关标签:
4条回答
  • 2020-12-08 00:06

    Look at urllib module:

    (with Python 3.1, in Python 2, use urllib2.urlopen instead) For retrieving cookies:

    >>> import urllib.request
    >>> d = urllib.request.urlopen("http://www.google.co.uk")
    >>> d.getheader('Set-Cookie')
    'PREF=ID=a45c444aa509cd98:FF=0:TM=14.....'
    

    And for sending, simply send a Cookie header with request. Like that:

    r=urllib.request.Request("http://www.example.com/",headers={'Cookie':"session_id=1231245546"})
    urllib.request.urlopen(r)
    

    Edit:

    The "http.cookie"("Cookie" for Python 2) may work for you better:

    http://docs.python.org/library/cookie.html

    0 讨论(0)
  • 2020-12-08 00:13

    Current answer is to use Requests module and the requests.Session object.

    • Quick Start; http://docs.python-requests.org/en/master/user/quickstart/#json-response-content
    • Sessions: http://docs.python-requests.org/en/master/user/advanced/#session-objects
        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)]
    

    You may need to pip install requests or pipenv install requests first.

    0 讨论(0)
  • 2020-12-08 00:17

    You should use the cookielib module with urllib.

    It will store cookies between requests, and you can load/save them on disk. Here is an example:

    import cookielib
    import urllib2
    
    cookies = cookielib.LWPCookieJar()
    handlers = [
        urllib2.HTTPHandler(),
        urllib2.HTTPSHandler(),
        urllib2.HTTPCookieProcessor(cookies)
        ]
    opener = urllib2.build_opener(*handlers)
    
    def fetch(uri):
        req = urllib2.Request(uri)
        return opener.open(req)
    
    def dump():
        for cookie in cookies:
            print cookie.name, cookie.value
    
    uri = 'http://www.google.com/'
    res = fetch(uri)
    dump()
    
    res = fetch(uri)
    dump()
    
    # save cookies to disk. you can load them with cookies.load() as well.
    cookies.save('mycookies.txt')
    

    Notice that the values for NID and PREF are the same between requests. If you omitted the HTTPCookieProcessor these would be different (urllib2 wouldn't send Cookie headers on the 2nd request).

    0 讨论(0)
  • 2020-12-08 00:17

    You can use in Python 2.7

    url="http://google.com"
    request = urllib2.Request(url)
    sock=urllib2.urlopen(request)
    cookies=sock.info()['Set-Cookie']
    content=sock.read()
    sock.close()
    print (cookies, content)
    

    and when sending request back

    def sendResponse(cookies): 
    import urllib
    request = urllib2.Request("http://google.com")
    request.add_header("Cookie", cookies)
    request.add_data(urllib.urlencode([('arg1','val1'),('arg1','val1')]))
    opener=urllib2
    opener=urllib2.build_opener(urllib2.HTTPHandler(debuglevel=1))
    sock=opener.open(request)
    content=sock.read()
    sock.close()
    print len(content)
    
    0 讨论(0)
提交回复
热议问题