Re: Help:How to post data to the admin form by urllib2 ?
On 27/07/2010 17:48, jerry wrote: > 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 > --- > > Looks like your browser isnt configured to > accept cookies. Please enable cookies, reload this page, and try > again. > > > style='display:none'> value='678f0507075332a87211dff43d6f9ee4' /> > > Username: name="username" id="id_username" /> > > > Password: name="password" id="id_password" /> > > > > > > > > > Could anyone give some suggestion? > 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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Help:How to post data to the admin form by urllib2 ?
On Wed, Jul 28, 2010 at 1:48 AM, jerrywrote: > 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 > > --- > > Looks like your browser isnt configured to > accept cookies. Please enable cookies, reload this page, and try > again. > > > style='display:none'> value='678f0507075332a87211dff43d6f9ee4' /> > >Username: name="username" id="id_username" /> > > >Password: name="password" id="id_password" /> > > > > > > > > > Could anyone give some suggestion? > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-us...@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@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 -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Help:How to post data to the admin form by urllib2 ?
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 --- Looks like your browser isnt configured to accept cookies. Please enable cookies, reload this page, and try again. Username: Password: Could anyone give some suggestion? -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.