how to set cookie in python mechanize
Asked Answered
F

5

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

Fishbowl answered 17/3, 2013 at 9:21 Comment(0)
E
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)
Endermic answered 8/12, 2013 at 2:55 Comment(0)
C
5

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

br.addheaders = [('Cookie','cookiename=cookie value')]
Clevey answered 2/6, 2015 at 14:34 Comment(0)
Q
1
import mechanize
import cookielib

br = mechanize.Browser()
cj = cookielib.CookieJar()
br.set_cookiejar(cj)
Quill answered 17/3, 2013 at 10:6 Comment(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.Fishbowl
There is another way to set the cookiejar: See this post.Quill
@Quill I think that this is the link (#3597357 that you intended to give.Endermic
S
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

Souza answered 13/4, 2021 at 18:26 Comment(0)
P
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)
Pyramidal answered 28/4, 2021 at 10:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.