Hướng dẫn how do i create a login and password in python? - làm cách nào để tạo thông tin đăng nhập và mật khẩu trong python?

Tôi chỉ là một lập trình viên mới bắt đầu ngay bây giờ và tôi đang cố gắng tạo một chương trình tên người dùng/mật khẩu. Đây là mã của tôi dưới đây:

username = 'Polly1220'

password = 'Bob'

userInput = input("What is your username?\n")

if userInput == username:
    a=input("Password?\n")   
    if a == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")

Bất cứ khi nào tôi chạy chương trình của mình và nhập mật khẩu chính xác như được hiển thị, nó luôn nói rằng mật khẩu không chính xác.

Hướng dẫn how do i create a login and password in python? - làm cách nào để tạo thông tin đăng nhập và mật khẩu trong python?

Đã hỏi ngày 20 tháng 6 năm 2017 lúc 21:33Jun 20, 2017 at 21:33

6

Bạn cần gán câu lệnh đầu vào thứ hai cho UserInput:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")

Đã trả lời ngày 20 tháng 6 năm 2017 lúc 21:35Jun 20, 2017 at 21:35

Hướng dẫn how do i create a login and password in python? - làm cách nào để tạo thông tin đăng nhập và mật khẩu trong python?

4

Bạn quên gán biến:

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")

Đã trả lời ngày 20 tháng 6 năm 2017 lúc 21:36Jun 20, 2017 at 21:36

Hướng dẫn how do i create a login and password in python? - làm cách nào để tạo thông tin đăng nhập và mật khẩu trong python?

developer_hatchdeveloper_hatchdeveloper_hatch

15,5k3 huy hiệu vàng41 Huy hiệu bạc72 Huy hiệu đồng3 gold badges41 silver badges72 bronze badges

Chúng tôi cũng đã bao gồm một chức năng đăng xuất cho phép người dùng đăng xuất bằng cách nhấp vào liên kết. Cuối cùng, chúng tôi đặt máy chủ thành

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
6, có nghĩa là chúng tôi đang lưu trữ cho người dùng bên ngoài (không phải trên cùng một máy tính) tại cổng 5000.Python on Ubuntu Server 18.04.

HTML và CSS

Bây giờ chúng tôi cần tạo trang web đăng nhập với HTML và CSS.

sudo apt-get update && sudo apt-get upgrade -y

Chạy đầu tiên:

Với tệp đó mở, sao chép và dán như sau:

Bây giờ chúng tôi đã thực hiện trang đăng nhập, hãy để tạo một trang chủ đơn giản.

sudo apt-get install python python-pip python-dev libmysqlclient-dev python-mysql.connector python3-mysql.connector -y

Bây giờ chúng tôi cài đặt Mariadb làm cơ sở dữ liệu của chúng tôi, nơi chúng tôi sẽ lưu trữ tên người dùng và mật khẩu băm:

sudo apt-get install mariadb-server -y

Sau khi được cài đặt, hãy chạy lệnh sau để thiết lập người dùng, cơ sở dữ liệu và bảng để kết nối mã Python của chúng tôi với:

Bây giờ chạy:

CREATE USER 'chooseAUserName'@'localhost' IDENTIFIED BY 'chooseAPassword';

Sau đó chạy:

GRANT ALL PRIVILEGES ON *.* TO 'chooseAUserName'@'localhost' WITH GRANT OPTION;
exit

Bây giờ chúng tôi sẽ cài đặt một số thư viện Python mà chúng tôi sẽ cần.

pip install flask
pip install flask-sqlalchemy
pip install flask-login
pip install passlib

Thiết lập người dùng

Bây giờ, chúng tôi sẽ thiết lập cơ sở dữ liệu người dùng và thêm một vài người dùng. Đầu tiên, kết nối với Mariadb bằng cách chạy:

Tiếp theo, hãy tạo một cơ sở dữ liệu mới có tên

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
5:

Sau đó, nhập cơ sở dữ liệu để chúng tôi có thể thêm một bảng sẽ giữ người dùng:

Và để tạo bảng:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
0

Bây giờ, để tạo người dùng, chúng tôi thực sự sẽ tạo một chương trình Python nhỏ để thêm người dùng vào cơ sở dữ liệu.

Bạn sẽ cần mở một phiên bản TMUX để chúng tôi có thể chỉnh sửa tệp và vẫn có chương trình Python chạy ở chế độ nền. Để làm điều đó, hãy làm theo loạt lệnh này:

Với tệp này mở, sao chép và dán mã sau:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
1

Bây giờ, đóng và lưu tệp bằng cách nhấn

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
6,
if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
7,
if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
8,
if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
8.

Bây giờ, khi vẫn còn trong TMUX chạy:

Sau đó thoát TMUX trong thời gian hiện tại bằng cách nhấn

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
0, sau đó
if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
1.

Bây giờ chạy:

và nhập IP kết quả vào một trình duyệt máy tính khác bằng cách nhập của bạn: 5000/ bây giờ bạn đã thêm người dùng đầu tiên của mình. Để thêm một lần chạy khác:

và thay đổi tên người dùng, mật khẩu và các trường email. Bây giờ hãy lưu và thoát như trên. Sau đó làm mới trang web trên máy tính khác và bạn đã thêm thành công người dùng khác. Bạn có thể thêm nhiều người dùng như bạn muốn bằng cách lặp lại quy trình này.

Khi bạn đã thêm tất cả người dùng mà bạn muốn, hãy chạy:

Sau đó, nhấn

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
2, tiếp theo là
if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
3.

Lập trình Python

Bây giờ chúng tôi đã thiết lập một vài người dùng, hãy để viết mã Python cho hệ thống đăng nhập và đi qua những gì nó làm.

Đầu tiên, hãy tạo một tệp mới bằng cách chạy:

Với tệp mở, bạn có thể sao chép và dán hoặc nhập mã sau:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
2

Trong tệp này, trước tiên chúng tôi sẽ nhập các thư viện Python mà chúng tôi sẽ cần. Sau đó, chúng tôi sẽ kết nối với chính cơ sở dữ liệu. Hàm đầu tiên xác định nếu người dùng được đăng nhập và nếu không, hãy hiển thị cho họ trang đăng nhập.

Sau đó, trong hàm

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
4, chúng tôi nhận được đầu vào của người dùng từ biểu mẫu web, băm mật khẩu của họ và xác minh nó dựa trên mật khẩu băm trong cơ sở dữ liệu của chúng tôi. Nếu điều này trả về đúng, người dùng hiện đã đăng nhập và chuyển hướng đến
if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
5 (trang chủ của chúng tôi).

Chúng tôi cũng đã bao gồm một chức năng đăng xuất cho phép người dùng đăng xuất bằng cách nhấp vào liên kết. Cuối cùng, chúng tôi đặt máy chủ thành

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
6, có nghĩa là chúng tôi đang lưu trữ cho người dùng bên ngoài (không phải trên cùng một máy tính) tại cổng 5000.

HTML và CSS

Bây giờ chúng tôi cần tạo trang web đăng nhập với HTML và CSS.

Chạy đầu tiên:

Then:

Với tệp đó mở, sao chép và dán như sau:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
3

Bây giờ chúng tôi đã thực hiện trang đăng nhập, hãy để tạo một trang chủ đơn giản.

Tạo và mở một tệp mới trong cùng một vị trí:

Sao chép và dán mã sau:

if userInput == username:
    userInput = input("Password?\n")   
    if userInput == password:
       print("Welcome!")
    else:
       print("That is the wrong password.")
else:
    print("That is the wrong username.")
4

Bây giờ chúng tôi có thể thoát khỏi thư mục mẫu bằng cách nhập

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
7 và chúng tôi đã sẵn sàng để chạy!

Kiểm tra mã của bạn

Bây giờ bạn đã viết mã, chúng tôi có thể chạy nó và thử nó trong trình duyệt của chúng tôi. Để làm điều này, chạy:

Bây giờ bạn có thể truy cập hệ thống bằng cách truy cập cùng một liên kết được sử dụng khi chúng tôi đang tạo người dùng:

if userInput == username
   userInput = input("Password?\n")   
    if userInput == password:
        print("Welcome!")
    else:
        print("That is the wrong password.")
else:
    print("That is the wrong username.")
8

Xin chúc mừng! Bây giờ bạn đã tạo một hệ thống đăng nhập người dùng cho một trang web trong Python! Bây giờ bạn có thể tạo trang web của riêng mình với các trang web riêng tư, nơi bạn có thể làm những việc như giữ một tạp chí riêng tư, lưu trữ ghi chú của bạn hoặc các tệp khác và nhiều hơn nữa. Hãy để trí tưởng tượng của bạn trở nên hoang dã và tạo ra một cái gì đó tuyệt vời.