6

After sending request to the server

    br.open('http://xxxx')
    br.select_form(nr=0)   
    br.form['MESSAGE'] = '1 2 3 4 5'
    br.submit()

I get the response title, which has set-cookie

Set-Cookie: PON=xxx.xxx.xxx.111; expires=Tue, 17-Mar-2015 00:00:00 GMT; path=/

Because mechanize seems to be not able to remember the cookie, so I want to set cookie for br. How can I do it?

    cj = mechanize....?
    br.set_cookiejar(cj)

I have no idea. Please help

5 Answers 5

6

I think that this should do what you want:

import Cookie
import cookielib
cookiejar =cookielib.LWPCookieJar()

br = mechanize.Browser()
br.set_cookiejar(cookiejar)
cookie = cookielib.Cookie(version=0, name='PON', value="xxx.xxx.xxx.111", expires=365, port=None, port_specified=False, domain='xxxx', domain_specified=True, domain_initial_dot=False, path='/', path_specified=True, secure=True, discard=False, comment=None, comment_url=None, rest={'HttpOnly': False}, rfc2109=False)
cookiejar.set_cookie(cookie)
5

you can also add a preexisting cookie manually with the addheaders method from mechanize's browser class.

br.addheaders = [('Cookie','cookiename=cookie value')]
1
import mechanize
import cookielib

br = mechanize.Browser()
cj = cookielib.CookieJar()
br.set_cookiejar(cj)
3
  • thanks. I tried it before posted my question. It does not work. I think I need to pass Set-Cookie: PON=xxx.xxx.xxx.111 to the br. But I dont know how.
    – John
    Mar 17, 2013 at 11:20
  • There is another way to set the cookiejar: See this post.
    – unutbu
    Mar 17, 2013 at 12:37
  • @unutbu I think that this is the link (stackoverflow.com/questions/3596857/… that you intended to give.
    – C R
    Dec 8, 2013 at 0:26
1

To set a cookie with python mechanize, first grab websites cookies and save them to file "cookies.lwp":

import mechanize, cookielib
cj = cookielib.LWPCookieJar()
br = mechanize.Browser()
br.set_cookiejar(cj)
br.open('https://www.somesite.com')
cj.save(filename="cookies.lwp", ignore_discard=False, ignore_expires=False)

You can now set any cookie in "cookies.lwp" to whatever value you want and then load them back into your browser:

cj.load(filename="modified_cookies.lwp", ignore_discard=False, ignore_expires=False)
br.set_cookiejar(cj)
br.open('https://www.yoursitehere.com')
for cookie in cj:
    print cookie

This video will walk you through it How To Modify Cookies with Python Mechanize

0

You can add cookies the better way using the set_simple_cookie function.

Considering your cookies are in json,

{
    "domain": ".example.com",
    "expirationDate": 1651137273.706626,
    "hostOnly": false,
    "httpOnly": true,
    "name": "SecureExampleId",
    "path": "/",
    "sameSite": "strict",
    "secure": true,
    "session": false,
    "storeId": null,
    "value": "v%3D2%26mac%..."

}
            
import http.cookiejar

cookiejar = http.cookiejar.LWPCookieJar()
br.set_cookiejar(cookiejar)

br.set_simple_cookie(cookie['name'], cookie['value'], cookie['domain'], cookie['path'])

response = br.open(url)
print(cookiejar._cookies)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.