Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I login to a website with Python?

How can I do it? I was trying to enter some specified link (with urllib), but to do it, I need to log in.

I have this source from the site:

<form id="login-form" action="auth/login" method="post">     <div>     <!--label for="rememberme">Remember me</label><input type="checkbox" class="remember" checked="checked" name="remember me" /-->     <label for="email" id="email-label" class="no-js">Email</label>     <input id="email-email" type="text" name="handle" value="" autocomplete="off" />     <label for="combination" id="combo-label" class="no-js">Combination</label>     <input id="password-clear" type="text" value="Combination" autocomplete="off" />     <input id="password-password" type="password" name="password" value="" autocomplete="off" />     <input id="sumbitLogin" class="signin" type="submit" value="Sign In" /> 

Is this possible?

like image 672
Bruno 'Shady' Avatar asked May 26 '10 05:05

Bruno 'Shady'


2 Answers

Maybe you want to use twill. It's quite easy to use and should be able to do what you want.

It will look like the following:

from twill.commands import * go('http://example.org')  fv("1", "email-email", "blabla.com") fv("1", "password-clear", "testpass")  submit('0') 

You can use showforms() to list all forms once you used go… to browse to the site you want to login. Just try it from the python interpreter.

like image 156
sloth Avatar answered Oct 23 '22 07:10

sloth


Let me try to make it simple, suppose URL of the site is www.example.com and you need to sign up by filling username and password, so we go to the login page say http://www.example.com/login.php now and view it's source code and search for the action URL it will be in form tag something like

 <form name="loginform" method="post" action="userinfo.php"> 

now take userinfo.php to make absolute URL which will be 'http://example.com/userinfo.php', now run a simple python script

import requests url = 'http://example.com/userinfo.php' values = {'username': 'user',           'password': 'pass'}  r = requests.post(url, data=values) print r.content 

I Hope that this helps someone somewhere someday.

like image 30
Tarun Venugopal Nair Avatar answered Oct 23 '22 06:10

Tarun Venugopal Nair