Creating a secure and unbreakable password is essential to protect your online accounts and personal information. In this tutorial, we will learn how to create a password generator using Python to generate strong and unique passwords.
Step 1: Setting up the HTML file
Create a new HTML file using a text editor like Notepad or Visual Studio Code. Save the file as password_generator.html
.
<!DOCTYPE html>
<html>
<head>
<title>Password Generator</title>
</head>
<body>
<h1>Python Beginner's Password Generator</h1>
<p>Click the button below to generate a strong and unbreakable password!</p>
<input type="text" id="password">
<button onclick="generatePassword()">Generate Password</button>
<script src="password_generator.js"></script>
</body>
</html>
Step 2: Creating the Python Password Generator
Create a new Python file and save it as password_generator.py
.
import random
import string
def generate_password(length=12):
characters = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(characters) for i in range(length))
return password
if __name__ == "__main__":
password = generate_password()
print(password)
Step 3: Setting up the JavaScript file
Create a new JavaScript file and save it as password_generator.js
. This file will handle generating a password using the Python script.
function generatePassword() {
fetch('http://localhost:5000/generate_password')
.then(response => response.json())
.then(data => {
document.getElementById('password').value = data.password;
});
}
Step 4: Running the Python Password Generator
To run the Python script, open a terminal or command prompt and navigate to the directory where the Python file is saved.
Run the following command to start a local server:
python -m http.server 5000
Now, when you open the HTML file in a web browser and click the "Generate Password" button, it will call the Python script to generate a strong and unbreakable password and display it in the input field.
Congratulations! You have successfully created a Python beginner’s password generator using HTML, Python, and JavaScript. Feel free to customize the length and complexity of the generated passwords by modifying the generate_password
function in the Python script.
Data Science Guide, Cheat Sheets and Free eBooks here: https://github.com/rocketingdatascience/RDS_Guide