我创建了一个脚本,它带有两个文本文件“username.txt”和“password.txt”。我的脚本尝试使用2个文本文件中提供的给定数据登录。我在username.txt文件中有user1@mymail.com到user10@mymail.com,在password.txt中有password1到password10。如果它成功登录它应该给我一个HTTP请求代码200,如果不成功它应该给我一个代码400.我的代码只运行第一行而不运行其余的。我该如何解决这个问题。这是我的代码。
import urllib, urllib2
user = open ('users.txt' , 'r')
password = open ('password.txt' , 'r')
pa = ''.join(password)
for users in user:
login_data = pa + users
base_url = 'http://mymail.com'
# login action we want to post data to
response = urllib2.urlopen(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()
这是我运行脚本时的输出。 Mark我已经设置了应该失败的用户,但我得到了一个代码200.
Date: Mon, 29 Jul 2013 14:54:59 GMT
Server: Apache
X-Powered-By: PHP/5.3.3
Set-Cookie: PHPSESSID=o3jlu86jgs7uj24fod107aps26; path=/
Cache-Control: no-cache
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
200
答案 0 :(得分:-1)
我所要做的就是确保我的循环回到嵌套循环的开头并使用.seek功能并加载所有密码并尝试将它们输出。
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://my-site.com'
#login action we want to post data to
response = urllib2.urlopen(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()