Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

how to retrieve all cookies with python

Posted on 2014-12-14
26
Medium Priority
?
222 Views
Last Modified: 2014-12-15
Hi

how to retrieve all cookies with python

I would want to get tokens key in cookies and I do not know how to get the full cookies

ty
0
Comment
Question by:hexo dark
  • 13
  • 13
26 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499100
'cookies' are normally stored in web browsers and each browser has it's own set of cookies for each web site that is visited.  I don't know what you mean by "tokens key".  More info here: http://en.wikipedia.org/wiki/HTTP_cookie
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40499133
mmm i got an exemple of script python juste how to adapt this for the web site

import cookielib, urllib2, urllib, fileinput, sys, re

def login(username,password):
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
	opener.addheaders =[('Referer', 'http://website.fr/index.php/connexion'),
						('User-Agent','Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0'),
						('Content-Type','application/x-www-form-urlencoded')]

	url ='http://website.fr/index.php/connexion'
	data = {'formname' : 'RpcApiUser_Login', 'fail_url' : 'http://website.fr/index.php/connexion', 'user' : username, 'login' : password}
	req = urllib2.Request(url, urllib.urlencode(data))
	res = opener.open(req)


try:
	with open('cookies.txt'): pass
except IOError:
	cookie_jar = cookielib.MozillaCookieJar('cookies.txt')
	cookie_jar.save()
if sys.argv[1] == 'no':
	print 'No cookies created.'
	sys.exit()
else:
	cookie_jar = cookielib.MozillaCookieJar('cookies.txt')
	cookie_jar.load()
	username = raw_input('Username: ')
	password = raw_input('Password: ')
	login(username,password)
	opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookie_jar))
	opener.addheaders =[('User-Agent','Mozilla/5.0 (Windows NT 6.1; rv:26.0) Gecko/20100101 Firefox/26.0'),
						('Connection','keep-alive')]
	url = 'http://website.fr/'
	req = opener.open(url)
	site = req.read()

	if re.search(username+'(?i)',site):
		print 'Login successful.'
		cookie_jar.save()

		for line in fileinput.input('cookies.txt',inplace =1):
			line = line.strip()
			if not 'c_visitor' in line:
				print line
	else:
		print 'Login failed.'
		sys.exit()

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499189
That code will only get the cookies that are set by that web site when you try to connect to it.  Is that what you want?
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:hexo dark
ID: 40499203
yes
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499209
Then I would just put in the correct web site addresses and try it.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40499216
It is what I made but it does not work

is the link http://animedigitalnetwork.fr/index.php/connexion
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499416
The login form on that page is more complicated than what is shown in your code.  You will need to include all the data required by the form to get it to work.

You might want to do a test version on a simpler page that you have control over so you can make something that works before you tackle that page again.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40499439
I would like to know what is information which I have to have to make a success(to succeed)? And how got this information
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499515
Look at the source for that page.  All of the 'input' values including the 'hidden' ones need to be submitted to the 'action' page.  There are 3 'forms' on that page but this is the one that you need to look at.  I'm not sure that your code will do a 'post' method to the web site.
<form action="/index.php/connexion" method="post" id="login-form" >
		<fieldset class="userdata">
	<p id="form-login-username">
		<label for="modlgn-username">Pseudo</label>
		<input id="modlgn-username" type="text" name="username" class="inputbox"  size="18" />
	</p>
	<p id="form-login-password">
		<label for="modlgn-passwd">Mot de passe</label>
		<input id="modlgn-passwd" type="password" name="password" class="inputbox" size="18"  />
	</p>
		<p id="form-login-remember">
		<label for="modlgn-remember">Se souvenir de moi</label>
		<input id="modlgn-remember" type="checkbox" name="remember" class="inputbox" value="yes"/>
	</p>
		<button type="submit" name="Submit" class="button" >Connexion</button><br/><a target="_blank" class="button login-createaccount" href="/index.php/component/account/?view=create&amp;return=aHR0cDovL2FuaW1lZGlnaXRhbG5ldHdvcmsuZnIvaW5kZXgucGhwL2Nvbm5leGlvbg==">
					Créer un compte</a>
				<input type="hidden" name="option" value="com_users" />
	<input type="hidden" name="task" value="user.login" />
	<input type="hidden" name="return" value="aHR0cDovL2FuaW1lZGlnaXRhbG5ldHdvcmsuZnIvaW5kZXgucGhwL2Nvbm5leGlvbg==" />
	<input type="hidden" name="20eb3eb9b0b326538448b5e479d0ccac" value="1" />	</fieldset>
	<ul>
		<li>
			<a target="_blank" href="/index.php/oubli-mot-de-passe">
			Mot de passe oublié ?</a>
		</li>
		<li>
			<a target="_blank" href="/index.php/identifiant-perdu">
			Pseudo oublié ?</a>
		</li>
	</ul>
	</form>

Open in new window

0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499517
Here is the Python page on using 'urllib2' :  https://docs.python.org/2/howto/urllib2.html
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40499565
ok

<div class="login-login">
    <h2 class="cr-header">Connexion</h2>
    <div class="login-tip1">
      Vous avez déjà un compte ? Connectez-vous ci-dessous.    </div>


    <form id="RpcApiUser_Login" method="post" action="https://www.website.com/?a=formhandler">
      <input type="hidden" name="formname" value="RpcApiUser_Login" />
                  <input type="hidden" name="fail_url" value="http://www.website.com/login" />
            <table>
        <tr>
          <th>E-mail ou nom d'utilisateur:</th>
          <td><input type="text" name="name" value="" /></td>
        </tr>
        <tr>
          <th>Mot de passe:</th>
          <td><input type="password" name="password" /></td>
        </tr>
                <tr>
          <td colspan="2">
            <a class="submit" href="#" onclick="$('#RpcApiUser_Login').submit();return false;">Connexion<span class="right"></span></a>
            <input type="submit" class="ie-hidden-submit" />
          </td>
        </tr>
      </table>
    </form>

Open in new window


It is the part of the code of the web page or the script connects normally and I resumed(took back) this script for an other web site (http://animedigitalnetwork.fr/index.php/connexion ) I try to understand this line

data = {'formname' : 'RpcApiUser_Login', 'fail_url' : 'http://website.fr/index.php/connexion', 'user' : username, 'login' : password}

Open in new window


to adapte this to animedigitalnetwork  ty for te link  :) and your help
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40499603
According to the Python page, the 'data' statement is a collection of the name/value pairs associated with the 'input' statements in the form.  Some form of that statement is used in all languages that are able to send a POST request to a web page.  In the first item, 'formname' : 'RpcApiUser_Login',  'formname' is the 'name' attribute from the 'input' statement and 'RpcApiUser_Login' will be the value you want to submit for it.  

However, "'user' : username, 'login' : password" are not part of the form above.  That form uses 'name' and 'password' for the names and you have to supply the values.  Like " 'name' : username, 'password' : password ".  Your script must match the name/value pairs that are found in each form you want to submit data to.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40500842
You would have any idea how I could place it because after several attempts = give nothing :(

Edit this site is a joomla logins !
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40501143
They don't have to let you log in and there are ways to prevent you from doing that using methods like the one you have been trying.  But you don't know (yet) how to code a successful login which is why I suggested you create a simple test site and get it working on that first.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40501324
The protections that you speak is contournables ? ok Am going to make a try with a joomla install
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40501349
I would really suggest something much simpler for initial testing.  I don't know what 'contournables' means.  More info here: http://www.w3schools.com/html/html_forms.asp and here: http://www.w3schools.com/php/php_forms.asp
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40501606
i have a question is possible to use firefox on terminal command to connect to a web site and get my information ? cookies files ...
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40501631
No.  Firefox runs in the GUI, not the terminal.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40501665
it partially works

import pycurl
import urllib
import StringIO

def test(debug_type, debug_msg):
    if len(debug_msg) < 300:
        print "debug(%d): %s" % (debug_type, debug_msg.strip())    

pf = {'username' : 'user', 'password' : 'pw' }
fields = urllib.urlencode(pf)
pageContents = StringIO.StringIO()

p = pycurl.Curl()
p.setopt(pycurl.FOLLOWLOCATION, 1)
p.setopt(pycurl.COOKIEFILE, './cookie_test.txt')
p.setopt(pycurl.COOKIEJAR, './cookie_test.txt')
p.setopt(pycurl.POST, 1)
p.setopt(pycurl.POSTFIELDS, fields)
p.setopt(pycurl.WRITEFUNCTION, pageContents.write)
p.setopt(pycurl.VERBOSE, True)
p.setopt(pycurl.DEBUGFUNCTION, test)
p.setopt(pycurl.URL, 'http://animedigitalnetwork.fr/index.php/connexion')
p.perform()

p.close() # This is mandatory.

pageContents.seek(0)
print pageContents.readlines()

Open in new window


juste how to get information of set cookies  the cookies is not complete
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40501679
To get the cookies, you may have to visit the page once in 'pycurl' before you try to login.  What you are showing is very similar to 'curl' in PHP.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40501706
I do not see how the script adjusts you can show me I at the beginning in this language and I simply want to have these complete cookies in a text file
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 40501719
You may to run the script twice, first as a 'GET' to load the cookies and second as a 'POST' to try to login.
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40501740
Would it be possible to get an example? I'm better at understanding if i can see it.
0
 
LVL 84

Accepted Solution

by:
Dave Baldwin earned 2000 total points
ID: 40501795
0
 
LVL 2

Author Comment

by:hexo dark
ID: 40502004
I give you points:) even if I did not succeed I abandon(give up) not but for the moment I can nothing more
0
 
LVL 2

Author Closing Comment

by:hexo dark
ID: 40502006
nikel
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question