2

I have a python script ( a random number generator ) and I want to create a site using HTML that uses the python script to give an input to the user which produces the random password. I am fairly new to programming and am not sure how to do this.

This is my code here

import random

chars = 'abcdefghijklmnopqrstuwxyz !@#$'

number = input("Number of passwords? - ")
number = int(number)

length = input("Password length - ")
length = int(length)

for f in range(number):
  password = ''
  for c in range(length):
    password += random.choice(chars)
  print(password)
9
  • Your question doesn't clearly convey what you are trying to achieve Commented Jul 27, 2020 at 21:34
  • I want a text box to appear, than it would give a question "Number of passwords? - " from the python script and the user gives an input, than the python script displays the random password in the text box on the html website. I hope that clears it up a bit. Commented Jul 27, 2020 at 21:42
  • I don't think it can be done in python but I have provided an answer below that does the same thing using Javascript. See if that helps Commented Jul 27, 2020 at 22:02
  • 1
    For web application with python, you may use django Commented Jul 27, 2020 at 22:07
  • 1
    is there a way that i can use a back-end sucha s django or flask to do this? Commented Jul 27, 2020 at 22:10

2 Answers 2

1

I am not exactly sure if you can do it on a website with python. There might be libraries that allow you to use python for the same but I don't think you can directly do it. However, you can do it using javascript as follows -

const chars = 'abcdefghijklmnopqrstuwxyz !@#$'
let numberOfPasswords = 0;

function passNum(value) {
  numberOfPasswords = value;
}

let lengthOfPassword = 0

function passLen(value) {
  lengthOfPassword = value;
}

let res = '';

function displayResult() {

  for (let i = 0; i < numberOfPasswords; i++) {
    for (let j = 0; j < lengthOfPassword; j++) {
      res += chars[Math.floor(Math.random() * Math.floor(chars.length))]
    }
    res+='\n'
  }
  document.getElementById('result').innerHTML = res;
}

document.getElementById("getPass").addEventListener("click", displayResult);
input,textarea{
  width:80%
}
<label for="fname">Number of passwords? - </label><br>
<input type="text" id="passnum" name="passnum" placeholder='Enter numer  of Passwords to be generated' onChange='passNum(this.value)'><br><br>
<label for="lname">Password length - </label><br>
<input type="text" id="passlen" name="passlen" placeholder='Enter desired length of Password' onChange='passLen(this.value)'><br><br>
<b>GENERATED PASSWORDS =></b><br>
<textarea rows="4" cols="50" id='result' placeholder='Generated passwords will be displayed here one on each line'>
</textarea><br>
<button id='getPass'>Generate password</button>

Sign up to request clarification or add additional context in comments.

6 Comments

This answer is definitely useful, and it works great, but I do see that it should be encouraged for the OA to use a Python lib for this usecase, as that is more what they were looking for and more broadly fits the tags
when i put this code in Note Pad and open it in chrome it doesn't work.
@FrancescoErichsen you have to put the javascript code inside script tags - <script> js code </script>. You can put this inside the <head> or right before the ending body tag
i added that in with the proper html format and nothing shows up on the page?
@FrancescoErichsen there must be some error in the way you have written code or while saving file/opening in browser or something. If it's working here, it must work when you save it in a notepad as html and open in browser
|
0

A purely Pythonic way, using Flask. Execute the code, open your browser, head to localhost:8080, enter inputs and press Go.

from flask import Flask, jsonify, request
import random
app = Flask(__name__)

@app.route('/')
def index():
    print('Verified Request made to Base Domain')
    resp = jsonify(status=200)
    return "\
    <form type='GET' action='/result'>\
    <label for='number'>Number</label>\
    <input type='text' name='number' />\
    <label for='length'>Length</label>\
    <input type='text' name='length' />\
    <input type='submit' value='Go' />\
    </form>\
    "
@app.route('/result', methods=['GET'])
def run():
    number = int(request.args['number'])
    length = int(request.args['length'])
    passwords = []
    chars = 'abcdefghijklmnopqrstuwxyz !@#$'
    for f in range(number):
        password = ''
        for c in range(length):
            password += random.choice(chars)
        passwords.append(password)
    return jsonify(passwords= passwords)

app.run('localhost', 8080, app)


Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.