write a piece of code that ask the user to set a password
- queries the user for his/her user name
- queries the user for his/her password twice, making sure the user enters the same password twice
- Allow Only 3 attempts to get a correct password
the rules for the password are
- at least one number
- at least one lower cap letter
- at least one upper cap letter
- allowed characters: numbers, letters, "_", "-" and "."
The above task should be done using expect concept. Since am new to python language can anyone help me in solving this task.
import pexpect
child = pexpect.spawn('ftp ftp.openbsd.org')
child.expect('Name .*: ')
child.sendline('anonymous')
child.expect('Password:')
I have tried till thid and donn't know how to proceed.
How about
child.sendline(raw_input('Password:'))
It prints Password: and asks the user for the password.
Related
Problem Description:
We have a User model where Phone number is been set as username and its unique=True,
We also have a field where to represent verified/unverified users. And we verify users by OTP verification to phone number. So now we need to add a new feature where user can replace his phone_number and then he will go to verification process and then we will change his number.
And also We need to preserve his previous number (which he already verified) until this new number is verified.
If for some reason user decides to go back without verification we delete newly added number and keeps his old number as username.
So what would be the best way to approach this problem
What I'm considering to have another Model linked with ForeignKey to User
class AlternativeNumber(models.Model):
user = User() # forignkey
number = Charfield
is_verfied = BooleanField
So we will be verifying users number and replacing his original number from his alternativenumber_set
Do you think we can simply it more. Do you think it might have some flaws or we go with a better appraoch ? thanks.
In my opinion you can add two new fields to the User class
alternative_number = CharField
is_aletrnative_verfied = BooleanField
which is really similar to your approach, but it is simpler because all the data is in one table. if a user did not verify his number then you can create a cron job that checks if the is_aletrnative_verfied and if it was false then delete whatever value is in the alternative_number just for the data to be consistent.
Hope you find this helpful!
We have been trying to upload users to cognito via the import jobs. However, seems like the phone_number attribute refuses to accept Indian mobile numbers. The format for phone number is like +919839999999. The phone_number_verified field is set to TRUE. Below is the error I receive:
[FAILED] Line Number 21 - The User Record contains an invalid phone number for the phone_number attribute.
From the documentation, cognito does say that after first two characters of country code, it looks for area code but I am not sure if that'll be the case when using cellphone numbers.
It seems if you are using google sheets, you have to re-convert the format to text of the cellphone fields or manually edit the csv. It works then
I have a sign in form where userid is email id of users or unique id given by us i.e in integer.
So i want regex for that text box to validate email or Id.
Means.
if someone enters
abc#abc.com
or
1024
then it is valid
and if someone is entering anything other than email or integer than it should show as invalid.
Thanks...
^(?:[\w.]+#[\w.]+|\d+)$
You can try this.Though Your requirements are not very clear.See demo.
http://regex101.com/r/zU7dA5/4
I am taking password inputs from user and planning to use passlib.hash library for encrypting the password .
I am able to encrypt the password successfully and their documentation states about the verify function usage, however I want to find out the original password entered , Is it possible ?
For example :
User entered a password : abcd
Password encrypted using passlib to a variable hash
How to decrypt the password hash and get value abcd ?
See the sample code below :
print "Enter a password "
passw = raw_input()
print "password entered is ", passw
hash = sha256_crypt.encrypt(passw)
retrivepass=?
Is it possible to retrive the original password ? If not what is the best alternative ?
Thanks in advance
Is it possible to retrieve the original password?
No. The purpose of a password hashing function is to make it possible for you to authenticate users without actually storing their passwords.
Hashing functions are one-way, so given the output, you cannot infer what the input was. Think of it like adding two numbers together. You know that 2 + 2 = 4, but given 4, you can't figure out that the two numbers that were added together were 2 and 2.
If not what is the best alternative?
You most likely don't need encryption. You need a good password hashing function, like Bcrypt or PBKDF2.
Store the hash then when the user would like access:
valid = False
while not valid:
passw = raw_input("Enter your password")
valid = sha256_crypt.verify(passw, hash)
Hashing is the process of encrypting standard blocks of data, which often results in the same length of output, with even infinitely long input. SHA256 encodes input data into 256 bits of output data.
You can always hash the password and store it. But the password cannot be retrieved back. The ideal way is to get the password again, hash it, and compare it with the stored hashed value.
I've read the Django documentation here:
http://docs.djangoproject.com/en/dev/ref/forms/validation/
I've also browsed a number of search results on Google and Stack Overflow, but I haven't been able to answer my questions below.
As an example, say I have a model named "Widgets" with a CharField named "product_name". Now say that I want to restrict the allowable characters in "product_name" to [a-zA-Z0-9] plus apostrophes, dashes and underlines (i.e. ' - _) and show a form error to the user if they enter a restricted character.
From the above research, I gather that I need to create a validation function somewhere to check for these characters.
My specific questions:
1. What is best practice as to where a validation function like this should live in my Django project?
2. From where do I call this validation function?
3. How do I show an error to the user if a "bad" character is entered?
4. Would someone be so kind as to post a sample validation function?
Thank you, I appreciate any help you can provide.
Go with Chefsmart's answer if you can. Here is an example of a sample validation function in case it helps:
class MyCustomInvoiceForm(forms.Form):
serial_number = forms.IntegerField(min_value = 1, initial = 1)
# Other fields. I am interested in serial_number.
def clean_serial_number(self):
serial_number = self.cleaned_data.get('serial_number')
if not serial_number:
return
return _my_custom_validation(serial_number)
def _my_custom_validation(self, serial_number):
serial_number = int(serial_number)
if not ((serial_number - 1) % 10 == 0):
message = "Serial number should be 1 or one greater than a multiple of 10 (Example: 1, 11, 21 etc.)"
raise forms.ValidationError(message)
return serial_number
This is a snippet of code from a project I did. The customer required some interesting verification logic for serial number.
If you are using a Django form, you have the option of using a RegexField for your product_name field. See http://docs.djangoproject.com/en/1.2/ref/forms/fields/#regexfield
That would be the cleanest approach for your situation.