Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #51665 > unrolled thread

script to Login a website

Started bywachkama@gmail.com
First post2013-07-31 08:33 -0700
Last post2013-08-01 12:31 -0700
Articles 9 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  script to Login a website wachkama@gmail.com - 2013-07-31 08:33 -0700
    Re: script to Login a website John Gordon <gordon@panix.com> - 2013-07-31 16:21 +0000
      Re: script to Login a website wachkama@gmail.com - 2013-07-31 09:50 -0700
        Re: script to Login a website Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-07-31 22:57 -0400
        Re: script to Login a website Dave Angel <davea@davea.name> - 2013-08-01 12:59 +0000
    Re: script to Login a website Joel Goldstick <joel.goldstick@gmail.com> - 2013-07-31 16:39 -0400
    Re: script to Login a website wachkama@gmail.com - 2013-08-01 06:48 -0700
      Re: script to Login a website Dave Angel <davea@davea.name> - 2013-08-01 14:18 +0000
    Re: script to Login a website wachkama@gmail.com - 2013-08-01 12:31 -0700

#51665 — script to Login a website

Fromwachkama@gmail.com
Date2013-07-31 08:33 -0700
Subjectscript to Login a website
Message-ID<ece5f6b0-16da-4c3a-83d1-6340cb10cb9d@googlegroups.com>
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
           
    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)
    response.read()
    print response.headers
    print response.getcode()

[toc] | [next] | [standalone]


#51667

FromJohn Gordon <gordon@panix.com>
Date2013-07-31 16:21 +0000
Message-ID<ktbdj7$im2$1@reader1.panix.com>
In reply to#51665
In <ece5f6b0-16da-4c3a-83d1-6340cb10cb9d@googlegroups.com> wachkama@gmail.com writes:

> 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 ?

How is the data in 'users.txt' and 'password.txt' organized?  Given the
filenames, I would expect that 'users.txt' contains one username on each
line, and 'password.txt' contains one password on each line, with the
first username belonging with the first password, the second username
belonging with the second password, and so on.

Is this correct?

If so, that raises the question of how to handle multiple usernames and
passwords.  Do you just want to use one, or are you supposed to use them
all somehow?

Anyway, to begin to solve your problem, I'd copy just the file-reading code
into a separate program and add plenty of print statements to make sure it
works correctly.  Once you have that fixed, then you can worry about the
web login stuff.

And when you read the contents of each file, be aware that the newlines
at the end of each line are included.  If you don't want these, be sure
to call the rstrip() method to remove traling whitespace.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#51671

Fromwachkama@gmail.com
Date2013-07-31 09:50 -0700
Message-ID<0bf56e63-a6d3-4c7a-a4e9-642351081311@googlegroups.com>
In reply to#51667
On Wednesday, July 31, 2013 12:21:59 PM UTC-4, John Gordon wrote:
> In <ece5f6b0-16da-4c3a-83d1-6340cb10cb9d@googlegroups.com> wachkama@gmail.com writes:
> 
> 
> 
> > 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 ?
> 
> 
> 
> How is the data in 'users.txt' and 'password.txt' organized?  Given the
> 
> filenames, I would expect that 'users.txt' contains one username on each
> 
> line, and 'password.txt' contains one password on each line, with the
> 
> first username belonging with the first password, the second username
> 
> belonging with the second password, and so on.
> 
> 
> 
> Is this correct?
> 
> 
> 
> If so, that raises the question of how to handle multiple usernames and
> 
> passwords.  Do you just want to use one, or are you supposed to use them
> 
> all somehow?
> 
> 
> 
> Anyway, to begin to solve your problem, I'd copy just the file-reading code
> 
> into a separate program and add plenty of print statements to make sure it
> 
> works correctly.  Once you have that fixed, then you can worry about the
> 
> web login stuff.
> 
> 
> 
> And when you read the contents of each file, be aware that the newlines
> 
> at the end of each line are included.  If you don't want these, be sure
> 
> to call the rstrip() method to remove traling whitespace.
> 
> 
> 
> -- 
> 
> John Gordon                   A is for Amy, who fell down the stairs
> 
> gordon@panix.com              B is for Basil, assaulted by bears
> 
>                                 -- Edward Gorey, "The Gashlycrumb Tinies"

Hi John 
let me answer your questions 
the user.txt file has one user name on each line and so does the password.txt. with this in mind each user will attempt to log in with all the passwords on password.txt file. when it gets to the end of the line it will go to the next user in users.txt and do the same i.e attempt to log in with all the passwords in the file.
So to your second question I will use all the users one after the other attempting to log in.
I am able to get each user to use "x" number of passwords in the password.txt that part works fine. The login section is where I am stuck ?

Sam

[toc] | [prev] | [next] | [standalone]


#51713

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2013-07-31 22:57 -0400
Message-ID<mailman.63.1375325859.1251.python-list@python.org>
In reply to#51671
On Wed, 31 Jul 2013 09:50:45 -0700 (PDT), wachkama@gmail.com declaimed the
following:

>let me answer your questions 
>the user.txt file has one user name on each line and so does the password.txt. with this in mind each user will attempt to log in with all the passwords on password.txt file. when it gets to the end of the line it will go to the next user in users.txt and do the same i.e attempt to log in with all the passwords in the file.

	I sure hope you aren't trying to access a site that detects failed
logins and eventually locks the account after /n/-consecutive failures.

	Trying lots of passwords with a single "username" could look very much
like a mechanized attempt to break into the account. {Bad enough I've got a
flash drive that will lock up if I miskey the unlock code 10 times in a
row; paranoia at work in the corporate environment -- cf: Apricorn Aegis
Secure Key}

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#51729

FromDave Angel <davea@davea.name>
Date2013-08-01 12:59 +0000
Message-ID<mailman.76.1375362009.1251.python-list@python.org>
In reply to#51671
wachkama@gmail.com wrote:

> On Wednesday, July 31, 2013 12:21:59 PM UTC-4, John Gordon wrote:

     <SNIP lots of double-spaced stuff>
>> 
>> How is the data in 'users.txt' and 'password.txt' organized?  Given the
>> 
>> filenames, I would expect that 'users.txt' contains one username on each
>> 
>> line, and 'password.txt' contains one password on each line, with the
>> 
>> first username belonging with the first password, the second username
>> 
>> belonging with the second password, and so on.
>> 
     <SNIP lots more>

> the user.txt file has one user name on each line and so does the password.txt. with this in mind each user will attempt to log in with all the passwords on password.txt file. when it gets to the end of the line it will go to the next user in users.txt and do the same i.e attempt to log in with all the passwords in the file.
> So to your second question I will use all the users one after the other attempting to log in.
> I am able to get each user to use "x" number of passwords in the password.txt that part works fine. The login section is where I am stuck ?

My guess is you've got a nested loop, with the outer loop going through
the user file, and the inner loop going through the password file.

You're attempting to do readline() or equivalent from the password
file in the inner loop, and once you've gone through the outer loop
once, there is nothing left in the password file.

userfile = open("user.txt")
passwordfile = open("password.txt")

for user in  userfile:
      for password in passwordfile:
            process this user/pw combination


If that's your logic, simply move the open line for password.txt into
the outer loop, so it gets reopened each time.

You could also do a seek() to get to the beginning of the file each
time, or you could preread all the passwords into a list, or ...

Please note that the buggy googlegroups has totally messed up your
quoting.  Either restrict the quoting to a couple of lines, or remove
all those extra blank lines.

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#51705

FromJoel Goldstick <joel.goldstick@gmail.com>
Date2013-07-31 16:39 -0400
Message-ID<mailman.57.1375303175.1251.python-list@python.org>
In reply to#51665
On Wed, Jul 31, 2013 at 11:33 AM,  <wachkama@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

[toc] | [prev] | [next] | [standalone]


#51731

Fromwachkama@gmail.com
Date2013-08-01 06:48 -0700
Message-ID<6ad4bcae-40ba-4fd1-9b88-f0072cc17764@googlegroups.com>
In reply to#51665
On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach...@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
> 
>            
> 
>     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)
> 
>     response.read()
> 
>     print response.headers
> 
>     print response.getcode()


This is how my out put looks like when I run the script

User1@mymail.com
Password1

User1@mymail.com
Password2

User1@mymail.com
Password3

User1@mymail.com
Password4

User2@mymail.com
Password1

User2@mymail.com
Password2

User2@mymail.com
Password3

User2@mymail.com
Password4

User3@mymail.com
Password1

User3@mymail.com
Password2

User3@mymail.com
Password3

User3@mymail.com
Password4

User4@mymail.com
Password1

User4@mymail.com
Password2

User4@mymail.com
Password3

User4@mymail.com
Password4

Date: Thu, 01 Aug 2013 13:45:07 GMT
Server: Apache
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=3523t5l1vhqisaabfss8ra5tv5; path=/
Cache-Control: no-cache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8

200

[toc] | [prev] | [next] | [standalone]


#51735

FromDave Angel <davea@davea.name>
Date2013-08-01 14:18 +0000
Message-ID<mailman.78.1375366743.1251.python-list@python.org>
In reply to#51731
wachkama@gmail.com wrote:

> On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach...@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

Please ignore myearlier post, as I somehow missed your post where you
showed the code.

Anyway, your problem is that you print the login_data, but you don't act
on it.  You should make the call to the login function right after the
print statement, rather than after the loops are done.  At that point,
all you have is the last user name and password.

All the following code should be moved to a function, and called from
the same level as the print.

>> 
>>            
>> 
>>     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)
>> 
>>     response.read()
>> 
>>     print response.headers
>> 
>>     print response.getcode()
>
=
-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#51759

Fromwachkama@gmail.com
Date2013-08-01 12:31 -0700
Message-ID<ed26a5aa-0b0f-47a7-b66f-1309558d7cf8@googlegroups.com>
In reply to#51665
On Wednesday, July 31, 2013 11:33:25 AM UTC-4, wach...@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
> 
>            
> 
>     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)
> 
>     response.read()
> 
>     print response.headers
> 
>     print response.getcode()

That defiantly Got me my results. Thank you. I put the code in login code into the loop and that has given me my expected results.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web