/    Sign up×
Community /Pin to ProfileBookmark

How do you create a cookie in Python?

“`
<html>
<body>

<form action = “/setcookie” method = “POST”>
<p><h3>Enter userID</h3></p>
<p><input type = ‘text’ name = ‘nm’/></p>
<p><input type = ‘submit’ value = ‘Login’/></p>
</form>

</body>
</html>
“`

<!– later on when someone lands on the page take the contents and write response cookie — >

“`
@app.route(‘/setcookie’, methods = [‘POST’, ‘GET’])
def setcookie():
if request.method == ‘POST’:
user = request.form[‘nm’]

resp = make_response(render_template(‘readcookie.html’))
resp.set_cookie(‘userID’, user)

return res
“`

to post a comment

5 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumSep 12.2022 — @SubhanUllah#1646939

Check if this answers your question:

https://stackoverflow.com/questions/46661083/how-to-set-cookie-in-python-flask

Please use code tags when posting code: `your code here` or triple backticks. I edited your posting accordingly.
Copy linkTweet thisAlerts:
@mobiosolutionsSep 13.2022 — Check this link for a complete solution for Cookie & Session use in Python:

https://www.worthwebscraping.com/how-to-use-cookies-and-session-in-python-web-scraping/
Copy linkTweet thisAlerts:
@JamesMerchantSep 20.2022 — To set cookies in Python you need to create it with the HTTP Header before going to the Content-type field. Assuming you want to set UserID and Password as cookies. You must have understood how to set cookies. Use the Set-Cookie HTTP header to set cookies. It is very easy to use and anyone can use it who has a basic knowledge of HTML codes. It is optional to set cookie attributes like Expires, Domain, and Path. It is notable that cookies are set before sending the magic line "Content-type:text/htmlrnrn.
Copy linkTweet thisAlerts:
@Sam_DevSep 20.2022 — #!/usr/bin/python<br/>
print "Set-Cookie:UserID = XYZ;rn"<br/>
print "Set-Cookie:Password = XYZ123;rn"<br/>
print "Set-Cookie:Expires = Tuesday, 31-Dec-2007 23:12:40 GMT";rn"<br/>
print "Set-Cookie:Domain = www.tutorialspoint.com;rn"<br/>
print "Set-Cookie:Path = /perl;n"<br/>
print "Content-type:text/htmlrnrn"<br/>
...........Rest of the HTML Content....


From this example, you must have understood how to set cookies. We use Set-Cookie HTTP header to set cookies.

It is optional to set cookies attributes like Expires, Domain, and Path. It is notable that cookies are set before sending magic line "Content-type:text/htmlrnrn.

Also check the official site: https://docs.python.org/3/library/http.cookies.html
Copy linkTweet thisAlerts:
@Prachi_01Oct 17.2022 — In Python 3, you can create and set cookies for a webpage using the in-built requests library as demonstrated below.

import requests

# This is a dictionary object where the key is the name of the cookie and the value is the contents of the cookie.


# This will be your own custom cookie



cookies = {'enwiki_session': '17ab96bd8ffbe8ca58a78657a918558'}

r = requests.post('http://wikipedia.org', cookies=cookies)

print(response.content)

In addition to this you can get all sorts of other information such as webpage content, headers, etc.

We use the Set-Cookie HTTP header to set cookies. It is optional to set cookie attributes like Expires, Domain, and Path.

For advanced operations on cookies it is advisable to use http.cookiejar module.

You can refer to this blog to learn more about Best Python Books for Beginners https://www.boardinfinity.com/blog/best-python-books-for-beginners/
×

Success!

Help @SubhanUllah spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...