简体   繁体   中英

Python - Run a script multiple times with different data

The script logs into a website (signin()) and then registers for an event (registerforevent()).

How do I make this script run multiple times and each time using a different email and password from a list to sign in and register?

Code:

import requests
import time
import random 

def signin():
    sess=requests.session()
    url = "https://www.DOMAINE.com"
    payload = {"username":email,"password":password}
    r = requests.post(url,json=payload)
    print(r.status_code)


def registerforevent():
    sess=requests.session()
    url2 = "https://www.DOMAINE.com"
    payload = {"username":email,"password":password}
    r = requests.post(url2,json=payload)
    print(r.status_code)

signin()
registerforevent()

Data file:

email1@email.com:Password123
email2@email.com:Password123
email3@email.com:Password123
email4@email.com:Password123
email5@email.com:Password123

You need to give your signin() and registerforevent() functions email and password arguments; for example:

def signin(email, password):
    ....

def registerforevent(email, password):
    ....

Then you can simply run your functions in a loop that iterates over your different emails and password pairs and input them into the functions. There are many ways to do such a loop and it will depend on how you are storing and accessing each unique email/password pair.

One example is if you had your credentials stored in a python dictionary where the key could be the email address and value the password, eg:

credentials  = {"joe@blogs.com": 'password1234', "jane@blogs.com": '1234password'}

for email, password in credentials.items():
    signin(email, password)
    registerforevent(email, password)

Note: credentials.items() for python 3.x and credentials.iteritems() for python 2.x

All you really need are some minor adjustments to your functions and a for loop. I won't explicitly give you the finished code, but here's the procedure:

Write signin and registerforevent such that they accept the address and the password as arguments. Loop over the lines in your file, split each line by : and call your two functions in each iteration. Pretty straight forward.

Alternatively, call your script from the command line with different sys.args .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM