script to Login a website

Joel Goldstick joel.goldstick at gmail.com
Wed Jul 31 22:39:27 CEST 2013


On Wed, Jul 31, 2013 at 11:33 AM,  <wachkama at gmail.com> wrote:
>
> I have created a script to log in a website. It gets its username and password from two files, then log's in with this credentials. My code is not showing me what username it is using to login from the file. And I am not sure if it is even opening up the url and prompting for login. I am stuck can someone help me ?
>
>
>
>
>
>     import urllib, urllib2
>
>     user = open ('users.txt' , 'r')
>     password = open ('password.txt' , 'r')
>
>     for users in user:
>         password.seek(0)
>         for pass_list in password:
>             login_data = users + '\n' + pass_list
>             print login_data
>
I think you will note that login_data is overwritten each password
loop.  In the end of all of the above you have the last users followed
by a newline, followed by the last pass_list
You might want to think about putting the code below in a function
that can be called after print login_data above if you want to check
each username and password combination.


>     base_url = 'http://mysite.com'
>     #login action we want to post data to
>     response = urllib2.Request(base_url)
>     login_action = '/auth/login'
>     login_action = base_url + login_action
>     response = urllib2.urlopen(login_action, login_data)

I don't think the above line provides login_data as specified by the
spec:   http://docs.python.org/2/library/urllib2.html#module-urllib2

It looks like data needs to be tuples

>     response.read()
>     print response.headers
>     print response.getcode()
>
> --

Once you correct the top of your code I recommend Requests module
since its easier to understand, simpler, and better documented than
the standard url stuff.  You can find it at
http://docs.python-requests.org/en/latest/

> http://mail.python.org/mailman/listinfo/python-list



-- 
Joel Goldstick
http://joelgoldstick.com



More information about the Python-list mailing list