How to Set Passwords with Python


Creating a program to manage usernames and passwords can help users with many identities keep track of their credentials. In this guide, we’ll show you how to use python to set passwords in your terminal.

This guide makes use of the getpass module in the Python Standard Library. The getpass module provides a secure way for the user to input a password. It does this by hiding their input from prying eyes and storing it as a string-like object. You can install it using pip:

pip install getpass

Now we’ll set up a system for entering passwords with Python. We’ll create a function that prompts users for their username and password and compares them with stored values. If they’re correct, we’ll grant them access; if not, we’ll ask them to try again.

import getpass

users = {‘max’:’password’,’joe’:’secret’,’julia’:’hunter2′}

def login():

username = input(“enter your username: “)

password = getpass.getpass(“enter your password: “)

if username in users and users[username] == password:

print(“Welcome,”,username)

else:

print(“Sorry, invalid”)

login()

I’m always on the lookout for ways to simplify my day-to-day life. I’ve been using Python for a while now, and I find that it can be used to make many tasks much easier. One task I recently decided to automate (and have been meaning to do for years) is setting a new password every month. The password has to meet some requirements:

Must be greater than 12 characters

Must include at least one capital letter

Must include at least one number

Must include at least one special character such as !$%^&*()_+{}:”<>?~|\

Should be easy to remember, so similar letters or numbers should not be next to each other

Should not contain any dictionary words or usernames

This program does all of this by creating a random password string that is then checked against zxcvbn, which is a password strength estimator. If the strength does not meet my requirements, then it creates another random string and so on until it finds a strong enough password. Once the program generates a strong enough password, it stores the password and other authentication information in an encrypted file using Keyring.

The following code is a python script that allows a user to set passwords for sites that require authentication. This code is completely open-sourced.

import os, sys

from random import choice

from string import letters

from operator import itemgetter

from sqlalchemy import create_engine, Column, Integer, String

from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import sessionmaker

Base = declarative_base()

class User(Base):

__tablename__ = ‘users’

id = Column(Integer, primary_key=True)

username = Column(String)

password = Column(String)

def connect():

engine = create_engine(‘sqlite:///database.db’, echo=False)

Base.metadata.create_all(engine)

Session = sessionmaker(bind=engine)

return Session()

def randompassword():

chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ01234567

It is often useful to have authentication information for different purposes. For example, if you are writing a script that needs to ping your home router and reboot it if there is an issue, you will need the login information for the router. Many devices have web interfaces, so you may need to store a username and password in order to log into them. In this blog we will look at how Python can be used to manage such passwords.

To keep this blog short we will assume that you are using MacOS or Linux, and that you want to use Python 3.7. If you are using Windows or need to install a different version of Python, see the appendix at the end of this post for instructions.

First we need to make sure that Python is installed on our machine. You can check by opening a terminal and typing python3 –version. If everything is working correctly you should see something like:

Python 3.7.1

import sys

def password():

pas = raw_input(“Enter password: “)

if pas == “password”:

print “Correct!”

else:

print “Incorrect!”

sys.exit()

password()

Python Exit Program

Python is a great language that provides us with many tools to help us automate our daily tasks. One of my favorite features of Python is the ability to run the language through command line and interact with the operating system. This is great for automation as you can write scripts to do common tasks and then just run them through the command line.

If you have ever used Windows, you should be familiar with the Windows Command Line, or CMD. The CMD is a text interface that allows users to input commands and get responses. A simple example of this would be typing in ‘dir’ into your command line which lists all folders from the current location. It also allows users to run programs by typing in the program name.

For example: if you want to open Google Chrome, type ‘chrome’ into your cmd prompt (be sure its installed first) and it will open up your browser!

Python works very similarly in that it allows users to access operating system functionality through code, but even better than Windows it allows you to save these functions into files and then run those files whenever you want! For example, if you have a file called hello_world.py and in that file contains one function print(‘Hello World’) then if you type python

import sys

This line imports the module named sys so that we have access to the exit() function.

password = “hello”

password = input(“Enter your password: “)

This creates a variable named password and sets it to hello. We then use a built-in function named input() which will output some text (in this case “Enter your password:”) and wait for the user to enter some text. The text the user enters is then stored in the password variable.

if password == “hello”:

print(“Logged in!”)

else:

print(“Incorrect Password”)

sys.exit()

Here we use an if statement to check if the password stored in our variable matches our desired password (“hello”). If it does match, it will output “Logged In!”, otherwise it will output “Incorrect Password” and then call the exit() function from our imported sys module which will terminate the program.


Leave a Reply

Your email address will not be published. Required fields are marked *