[Help] how to save user cookies and credentials while creating bot in python

hpscool

Junior Member
Joined
Jul 19, 2010
Messages
186
Reaction score
54
Hi,
I was learning python for few weeks and was designing a bot that registers account and then save cookies for further usage.
How to deal with multiple user accounts and cookies.Where to store them.Any experianced programmer please help me

regards
 
Finally someone....well i can create accounts and all othr...but i m confused over how bots save cookies and stuff....i mean for example there was a youtube comment bot here (no longer on marketplace) which use cookies and http requests to upvote comments and add new comments,, are they storing the cookies in database
 
Well thanks that is interesting read
 
Still anyone who done it before with mass accounts?
 
Looked at this to dump them to a file?
https://stackoverflow.com/questions/13030095/how-to-save-requests-python-cookies-to-a-file
Yes -- dump them into a file.
  • The best kind of file to use would be a Sqlite database (for most use cases), since they're designed to be read from and written to. SQL databases (including Sqlite) are easy to work with in Python and there are built in Python libraries that work well for SQL reading/writing.
  • Of course, you can use any kind of file. It's possible to read/write to a .txt file or a .csv/.xlsx. Although those types of files are generally going to be much harder to work with for storing/retrieving data for a bot.
Basically, when you save cookie data (and other data about accounts), you save it to a database/other file. Then when the bot goes to use that account, it pulls a copy of the cookie data from the DB (or wherever) and puts it into the https://developer.mozilla.org/en-US/docs/Web/HTTP/Cookies before making requests. Or if you're using a browser emulator like Selenium, then you'd set it in the driver's preferences while creating a new driver instance (and then the emulated browser would put the cookie information into the HTTP request for you).
 
Well thats what my doubt? Actually your answer cleared a lot of it....But one more confirmation///...If i ahve 100 accounts then i have to store the cookie data into a database
 
Also can i pm you ?
 
You can also initialize it with your own attributes and generate an appropriate HTTP header using the output() method. Here's an example for setting a cookie:

#!/usr/bin/env python

import Cookie
import datetime
import random

expiration = datetime.datetime.now() + datetime.timedelta(days=30)
cookie = Cookie.SimpleCookie()
cookie["session"] = random.randint(1000000000)
cookie["session"]["domain"] = ".jayconrod.com"
cookie["session"]["path"] = "/"
cookie["session"]["expires"] = \
expiration.strftime("%a, %d-%b-%Y %H:%M:%S PST")

print "Content-type: text/plain"
print cookie.output()
print
print "Cookie set with: " + cookie.output()

You can set more than cookie at a time using this module (it refers to each one as a "morsel").

This next example shows how to read the same cookie:

#!/usr/bin/env python

import Cookie
import os

print "Content-type: text/plain\n"

try:
cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
print "session = " + cookie["session"].value
except (Cookie.CookieError, KeyError):
print "session cookie not set!"
 
Well im using requests in python.Its acutually simple and robust...But my problem is how to deal with 100s of cookies..Should i use a database?..if so can you guide me to somrgood guides?
 
Well thats what my doubt? Actually your answer cleared a lot of it....But one more confirmation///...If i ahve 100 accounts then i have to store the cookie data into a database

There's no specific volume of accounts at which you have to use a database. In theory, FaceBook could store all of it's data in .txt files duct taped together. It's not so much the volume of accounts that makes it worthwhile to use a database instead of some other file type; it's more that it's easier to work with in general.

Writing to a database is fairly simple. You execute some query like:
"UPDATE tumblr_accounts SET cookie_data=something WHERE id=12"

And then execute it with a one-line built-in Python function.

As opposed to writing to a CSV or text file or something else where you're going to end up writing a bunch of custom functions to parse and update the file, since they're not really designed to be interacted with in that way.

Also can i pm you ?
If you post in the thread I'll probably reply to it.
 
Yeah you just need to store cookies and they are nothing but text, so you can use txt files and avoid using any database altogether.
And retrieve them when you need them.

So I would make a folder say cookies and in that I would make files named as username of acc containing the cookie inside it .
 
Yeah you just need to store cookies and they are nothing but text, so you can use txt files and avoid using any database altogether.
And retrieve them when you need them.

So I would make a folder say cookies and in that I would make files named as username of acc containing the cookie inside it .
Thanks for the reply....I solved the issue using pickles in Python.As u suggested saved the cookies as username as pickle file
 
Back
Top