Shtoops Shtoops - 1 year ago 152
Python Question

Grab username:password from text file, then post into login form

I have a text file with usernames and passwords. The text file is formatted like this:


I want to grab the first line, split it to "username1" and "password1", and then post to this:

br.form['login'] = 'username1'
br.form['passwd'] = 'password1'

After that, I want it to repeat and move onto username2:password2.

How can this be done?

wim wim
Answer Source

Assuming no : characters in your usernames or passwords.

with open('myfile.txt') as f:
  credentials = [x.strip().split(':') for x in f.readlines()]

for username,password in credentials:
  # your code here 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download