Monday, May 19, 2014

Generate random password in Python

To generate random password in Python

import string
import random
from random import sample

def create_password(self):
        """
        create a random password
        """
        rand = string.ascii_letters + string.digits
        length = 8
        passwd = ''.join(sample(rand, length))
        return passwd

Ex:
>>> import string
>>> import random
>>> from random import sample
>>> rand = string.ascii_letters + string.digits
>>> length = 8
>>> passwd = ''.join(sample(rand, length))
>>> passwd
'8fK6ZR0q'
>>>

Labels: , ,


0 comments: Post Yours! Read Comment Policy ▼
PLEASE NOTE:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

 
Twitter Facebook RSS YouTube Google
© 2014 | Distributed and Designed By Jasad Moozhiyan