dimanche 26 janvier 2020

My Python code skips the function when being called the second time [closed]

I've been trying to figure this out on my own for a while now. I literally do not have the slightest clue as to why my code skips the block() function when it's being called from the usernameLogin() function.

You can see the tries variable too. This should execute the block() function when the variable passes over 3 but does not. This is my first time sending a question on here so any help will be much appreciated, Many thanks.

Here's the code:

import socket
import time
import sys
import os

tries = 0

def server():

    def block():
        #get host ip
        ipBlock = socket.gethostbyname(socket.gethostname())
        #create black list
        ipBlackList = open("ipBlackList.txt", "w")
        ipBlackList.close()

        #read black list
        ipBlackList = open("ipBlackList.txt", "r")
        isBlocked = ipBlackList.readlines()
        ipBlackList.close()

        #disable account for 60 seconds
        if ipBlock in isBlocked:
            def countdown(t):
                while t:
                    mins, secs = divmod(t, 60)
                    timer = '{:02d}:{:02d}'.format(mins, secs)
                    print('Your account has been temporarily disabled. Please try again in '+timer, 
                    end=" seconds.\r")
                    time.sleep(1)
                    t -= 1
                    os.remove('ipBlackList.txt')
                    print('\nYour account has been re-enabled. You may log in now.')
                    time.sleep(2)
                t = 60
                countdown(int(t))
    block()

    print("Enter administrator login details: ")
    def usernameLogin():
        #global vars
        global tries

        if tries == 3:
            #read black list
            ipBlackList = open("ipBlackList.txt", "r")
            isBlocked = ipBlackList.readlines()
            ipBlackList.close()
            #get host ip
            ipBlock = socket.gethostbyname(socket.gethostname())
            if ipBlock not in isBlocked:
                ipBlackList = open("ipBlackList.txt", "w")
                ipBlackList.write(ipBlock)
                ipBlackList.close()
                block()

        username = input("\nUsername: ")
        #check if username exists
        usernames = open("usernames.txt", "r")
        checkUser = usernames.readlines()
        usernames.close()

        if username not in checkUser:
            print("That username doesn't exist. Please try again.")
            tries += 1
            print(tries)
            time.sleep(2)
            usernameLogin()
        print("im in")
        time.sleep(3)

    usernameLogin()

server()

Aucun commentaire:

Enregistrer un commentaire