简体   繁体   中英

Python - while loop ends when it shouldn't

Python noob here. I am writing a simple client/server program where a user is asked to input a name, username, email address, password. This information is sent to the server, who checks if there is an entry in a text file for this user already. If there is, it is supposed to send a message back saying that this user already exists, asking the user to try again.

I set a variable called flag to False. I check the user information against the text file, and if no match is found in the file, I set the Flag to true. I then have an if statement that says if flag is True, write the user information in the file.

However, when I enter duplicate information, it sends back the appropriate "user already exists" message, but writes the duplicate information in the UserProfile.txt file anyway. I keep re-writing my loop and if statements differently to see if it will make a difference, but no matter what, I hit this same problem.

from socket import *
from datetime import datetime
#Create a welcome socket bound at serverPort
serverPort = 12009
serverSocket = socket(AF_INET,SOCK_STREAM)
serverSocket.bind(('',serverPort))
serverSocket.listen(10)
print ('The Hello Name server is ready to receive')
accessTime = datetime.now();
print("Access time is", accessTime);
flag = False

while 1:

    while not flag:
        connectionSocket, addr = serverSocket.accept()
        #Wait for the hello message 
        sentence1 = connectionSocket.recv(1024)
        print("From", addr,sentence1.decode('ascii'))
        #Ask for name if a hello message is received
        if(sentence1.decode('ascii').upper() == "HELLO"):
            returnMessage1 = "Please provide the requested information."
        connectionSocket.send(returnMessage1.encode()) 
        #Wait for the name
        sentence2 = connectionSocket.recv(1024)
        fullName = sentence2.decode('ascii')
        #Wait for the email
        sentence3 = connectionSocket.recv(1024)
        email = sentence3.decode('ascii')
        #Wait for the username
        sentence4 = connectionSocket.recv(1024)
        userName = sentence4.decode('ascii')
        #Wait for the password
        sentence5 = connectionSocket.recv(1024)
        password = sentence5.decode('ascii')



        for line in open("UserProfile.txt").readlines():
            if line.find(userName) > -1:    #found the username in this record
                returnMessage3 = "Username already exists, please try again" #reject username
                connectionSocket.send(returnMessage3.encode())
            if line.find(fullName) > -1 and line.find(email) > -1:
                returnMessage4 = "Account already exists for this person, please try again" #reject email                 
            else:
                flag = True

    if flag:            

        #Prepare the access record with information separated by tab key
        userAccount = userName+"\t"+password+"\t"+fullName+"\t"+email+"\n"
        #Append the access record into accessRecord.txt
        output_file = open("UserProfile.txt", "a")
        output_file.write(userAccount)
        output_file.close()
        #Respond the client with the access information
        returnMessage2 = "Registration Successful"
        connectionSocket.send(returnMessage2.encode())

    connectionSocket.close()                #Close the connection

When you find the user name already exists, you send back the reply, but you don't STOP.

Try putting a break in your if block:

for line
    if user name in line
        send message about already exists
        break - out of for loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM