script to Login a website

W

wachkama

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 sureif 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()
 
J

John Gordon

In said:
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.
 
W

wachkama

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

(e-mail address removed) 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
 
J

Joel Goldstick

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 cansomeone 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/
 
D

Dennis Lee Bieber

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}
 
D

Dave Angel

On Wednesday, July 31, 2013 12:21:59 PM UTC-4, John Gordon wrote:
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.
 
W

wachkama

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

(e-mail address removed)
Password1

(e-mail address removed)
Password2

(e-mail address removed)
Password3

(e-mail address removed)
Password4

(e-mail address removed)
Password1

(e-mail address removed)
Password2

(e-mail address removed)
Password3

(e-mail address removed)
Password4

(e-mail address removed)
Password1

(e-mail address removed)
Password2

(e-mail address removed)
Password3

(e-mail address removed)
Password4

(e-mail address removed)
Password1

(e-mail address removed)
Password2

(e-mail address removed)
Password3

(e-mail address removed)
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
 
D

Dave Angel

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.
=
 
W

wachkama

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 cansomeone 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top