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

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
John
  • 3,888
  • 11
  • 46
  • 84

5 Answers5

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)
Sukhwinder
  • 115
  • 1
  • 1
  • 7
C R
  • 2,182
  • 5
  • 32
  • 41
5

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

br.addheaders = [('Cookie','cookiename=cookie value')]
neif
  • 480
  • 7
  • 13
1
import mechanize
import cookielib

br = mechanize.Browser()
cj = cookielib.CookieJar()
br.set_cookiejar(cj)
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • 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 '13 at 11:20
  • There is another way to set the cookiejar: See [this post](http://stackoverflow.com/a/6681299/190597). – unutbu Mar 17 '13 at 12:37
  • @unutbu I think that this is the link (http://stackoverflow.com/questions/3596857/how-do-i-manually-add-more-cookies-to-a-session-which-already-has-cookies-set-in] that you intended to give. – C R Dec 08 '13 at 00: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

miguelraiz
  • 64
  • 3
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)
Sreekant Shenoy
  • 1,420
  • 14
  • 23