Simulation Forum Login

structure

 

 starts.py

# Author:Winter Liu is coming!
# 启动文件
import os
import sys
import json

BASE_PATH = os.path.dirname(os.path.dirname(__file__))
sys.path.append(BASE_PATH)

from conf import settings
from core import src

# 创建用户登录信息文件
with open(settings.STATUS_PATH, "w") as fp:
    json.dump({"status": False, "username": "", "UserPwd " : " " }, fp) 

# monitor user information file exists, does not exist, create a model using the json 
IF  not os.path.exists (settings.USER_PATH): 
    with Open (settings.USER_PATH, " w " ) AS FP: 
        The json.dump ({}, FP) 

# can only be started in this module 
IF  the __name__ == ' __main__ ' : 
    src.start ()

settings.py

import os

BASE_PATH = os.path.dirname(os.path.dirname(__file__))
STATUS_PATH = BASE_PATH + "/" + "db" + "/" + "status"
USER_PATH = BASE_PATH+"/"+"db"+"/"+"user_data"

src.py

# Author: Liu Winter IS Coming! 
# Main logic 
Import json
 from lib Import the Common
 from conf Import Settings 


DEF Start ():
     the while True:
         Print ( "" " Please enter a number 1-8: 
        1. Log in #### 
        2. #### Registered account number 
        3. #### Article 
        4. #### Comment 
        5. #### 日记
        6. #### Favorites 
        7. #### 送
        8. #### Exit "" " ) 
        choice = input ( " >>> " ) .strip ()
        if choice == "1":
            login()
        elif choice == "2":
            register()
        elif choice == '3':
            article()
        elif choice == '4':
            comment()
        elif choice == '5':
            diary()
        elif choice == "6":
            favorite()
        elif choice == "7" : 
            log_out () 
        elif choice == ' 8 ' :
             break 


@ common.auth 
def login ():
     pass 


def register (): 
    with open (settings.USER_PATH, " r " ) as f: 
        data = json.load (f )
     while True:
         print ( " Start registration, the user name consists of numbers and letters, the password length is 6-14 digits! " ) 
        username = input ( " Please enter the user name: " ) .strip ()
         if  notusername.isalnum ():
             continue 
        if username in data:
             print ( " Username already exists! " )
             continue 
        userpwd = input ( " Please enter password: " ) .strip ()
         if len (userpwd) <4 or 14 < len ( userpwd):
             continue 
        print ( " Before the user joins: " , data) 
        data [username] = userpwd
         print ( "After the user joins: " , data)
         #Note that rewriting user files 
        with open (settings.USER_PATH, " w " ) as f: 
            json.dump (data, f) 
        break 
    f.close () 

    print ( " Registration completed " ) 


@ common.auth 
def article ():
     print ( " Welcome to the article page! " ) 

@ common.auth 
def comment ():
     print ( " Welcome to the comment page! " ) 

@ common.auth 
def diary ():
     print ( " Welcome to the diary page! " ) 

@common .auth 
deffavorite ():
     print ( " Welcome to the favorite page! " ) 

def log_out (): 
    with open (settings.STATUS_PATH, " w " ) as fp: 
        json.dump ({ " status " : False, " username " : "" , " userpwd " : "" }, fp)
     print ( " Logout! " )

common.py

# Author: Liu Winter IS Coming! 
Import json
 from conf Import Settings 


# decorators, login and download the monitoring, detection by file status and user login name correct status 
DEF auth (f):
     DEF Inner (Arg *, ** kwargs): 
        with open (settings.STATUS_PATH, " r " ) as fps: 
            status_dict = json.load (fps)
         if status_dict [ " status " ]:
             print ( " logged in " ) 
            ret = f (* arg, ** kwargs)
             returnRET
         the else :
             IF USER_LOGIN (): 
                RET = f (Arg *, ** kwargs)
                 return RET
             the else :
                 Print ( " ! username or password wrong three times " ) 

    return Inner 


# Login 
DEF USER_LOGIN (): 
    with Open (settings.USER_PATH , " r " ) as fp: 
        user_data = json.load (fp)
     for i in range (3):   # three-time input restriction 
        username = input (" Please enter user name: " ) .strip () 
        userpwd = input ( " Please enter password: " ) .strip ()
         if username in user_data and user_data [username] == userpwd:
             print ( " Login successful " ) 
            status_dict = { " status " : True} 
            status_dict [ " username " ] = username 
            status_dict [ " userpwd " ] = userpwd
            with open (settings.STATUS_PATH," w " ) as fp: 
                json.dump (status_dict, fp) 
            return True
         else :
             print ( " Username or password is wrong! " )
     else :
         return False

 

Guess you like

Origin www.cnblogs.com/nmucomputer/p/12752581.html