Help:How to post data to the admin form by urllib2 ?

184 views
Skip to first unread message

jerry

unread,
Jul 27, 2010, 11:48:16 AM7/27/10
to Django users
Hi:
I build a website and want to login the admin form by python code. I
have disabled the CSRF in my project and use urllib2 to post data.
here are my codes:
----------------------------------------------------------------------------------------------------------------
import urllib
import urllib2
import cookielib

cj = cookielib.CookieJar()
url_login ='http://127.0.0.1:8000/admin/'
body =
{'csrfmiddlewaretoken':'f85a33be11bd85108a1030fcce96a5ea','username':'root','password':'mypass','this_is_the_login_form':'1'}
opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-agent','Mozilla/4.0 (compatible; MSIE 7.0;
Windows NT 5.1)')]
urllib2.install_opener(opener)
req=urllib2.Request(url_login,urllib.urlencode(body))
u=urllib2.urlopen(req)
print u.read()
print urllib2.urlopen("http://127.0.0.1:8000/admin/sites/
site/").read()
----------------------------------------------------------------------------------------------------------------

but the codes don't seem to work,here is some part of the response
-----------------------------------------------------------------------------------------------
....
<p class="errornote">Looks like your browser isn&#39;t configured to
accept cookies. Please enable cookies, reload this page, and try
again.</p>

<div id="content-main">
<form action="/admin/" method="post" id="login-form"><div
style='display:none'><input type='hidden' name='csrfmiddlewaretoken'
value='678f0507075332a87211dff43d6f9ee4' /></div>
<div class="form-row">
<label for="id_username">Username:</label> <input type="text"
name="username" id="id_username" />
</div>
<div class="form-row">
<label for="id_password">Password:</label> <input type="password"
name="password" id="id_password" />
<input type="hidden" name="this_is_the_login_form" value="1" />
</div>
<div class="submit-row">
<label>&nbsp;</label><input type="submit" value="Log in" />
</div>
</form>
....

Could anyone give some suggestion?

Rory Hart

unread,
Jul 28, 2010, 8:10:34 AM7/28/10
to django...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


Hey Jerry

The issue is in how Django admin tests to see if you accept cookies. Taking a quick look in django.contrib.admin.view.decorators the decorator staff_member_required tests to see if the cookie "testcookie=worked" exists. If you set that cookie your code should work.

Hope that helps.

--
Rory Hart
http://roryhart.net

Benedict Verheyen

unread,
Jul 28, 2010, 9:06:02 AM7/28/10
to django...@googlegroups.com

This is how i login to my site.
If you change the url you can test it from a shell to see if
it works for the admin login.

import urllib2,urllib
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
urllib2.install_opener(o)
id='Admin'
pw='xyz'
p=urllib.urlencode({"username" : id, "password" : pw})
f=o.open("http://calltracking:8000/accounts/login/", p)
data=f.read()
f.close()

Regards,
Benedict

Reply all
Reply to author
Forward
0 new messages