0

出现错误:不允许发送或接收数据的请求,因为套接字未连接并且(使用 sendto 调用在数据报套接字上发送时)在服务器和客户端服务器之间登录时使用带有 tkinter 的套接字时未提供地址:

    import socket
import pickle
import datetime


# Socket settings
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # socket setting
my_socket.bind(('0.0.0.0', 1024)) # binding the server ip
my_socket.listen(10) # how much people can join the server
client_socket, client_address = my_socket.accept() # accepting the people if the server is available
print("Server Open") # prints that the server is open

# Functions

def get_users():
    # get from the login_users.txt all users & passwords
    # format in file - username|password
    logs = []
    with open('login_users.txt', 'r') as f:
        for line in f:
            cur = line.strip('\n\r')
            cur = cur.split("|")
            logs.append(cur)
        f.close()
    return logs

def get_rank(user):
    # get from the ranks.txt all users & passwords
    # format in file - username|password
    ranks = []
    with open('ranks.txt', 'r') as f:
        for line in f:
            cur = line.strip('\n\r')
            cur = cur.split("|")
            ranks.append(cur)
        f.close()
    for r in ranks:
        if r[0] == user:
            return r[1]

def add_to_logs(user, line):
    # add attempt to record (logs_.txt)
    now = datetime.datetime.now()
    with open('logs_.txt', 'a') as f:
        f.write(line + " " + str(now) + "\n")
        f.close()

def user_exist(user):
    for log in logs:
        if log[0] == user:
            return True

def check(user, password):
    # check the password that the user typed
    # if there are 3 failed tried disable the login button
    global logs
    global atmp
    global null_state
    global login_state
    global logged_user
    global status
    # get text from boxes:
    user = user.get()
    password = password.get()
    print("user: ", user)
    print("pass: ", password)
    # add attempt to logs_ file
    add_to_logs(user, f"{user} has tried to log in.")
    atmp += 1
    if user_exist(user):
        null_state = False
        for log in logs:
            if log[0] == user and log[1] == password:
                # pop up login succeed
                login_state = True
                logged_user = user
                status = True

if __name__ == '__main__':
    btpressed = pickle.loads(my_socket.recv(1024))
    if btpressed:
        client_socket.send(pickle.dumps([status]))
        username, password = pickle.loads(my_socket.recv(1024))  # Getting data from the server
        status = check(username, password)
        print(status)

客户:

    import socket
import pickle
import tkinter as tk
import tkinter.messagebox as mb
import tkinter.font as tkFont

user_send = "nothing"
pass_send = "nothing"

# Socket settings
my_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # setting the socket
my_socket.connect(('127.0.0.1', 1024))  # joining the server
print("Connected to the server successfully!")

def check_null(user, password):
    global null_state
    # check for the username and password in the file:
    if user == "" or password == "" or user == "" and password == "":
        null_state = True
        fail_msg = "Please make sure you have entered username and password in both fields!"
        mb.showinfo(title="Enter username and password", message=fail_msg)

#def display_system():
    #root1.title("Secure4U")
    #root1.geometry("1200x600")
    #fontStyle = tkFont.Font(family="Lucida Grande", size=20)
    #show_user_logged = tk.Label(root1, text="Hello " + logged_user + ",", font=fontStyle)
    #show_user_logged.pack(side="top", anchor="nw")
    #user_rank =
    #show_user_rank = tk.Label(root1, text="Rank: " + str(user_rank), font=fontStyle)
    #show_user_rank.pack(side="top", anchor="nw")
    #root1.mainloop()  # run tkinter display

def display_login():
    global log_button
    # initiate and display all objects on screen
    root.title("Secure4U Login")
    root.geometry("450x250")
    fontStyle = tkFont.Font(family="Lucida Grande", size=20)

    # user name
    user_label = tk.Label(root, text="Username:", font=fontStyle)
    # user_box = tk.Text(root, bg="grey", height=1, width=20, font=fontStyle)
    user_box = tk.Entry(root, bg="grey", font=fontStyle)
    user_label.pack()
    user_box.pack()

    # password
    pass_label = tk.Label(root, text="Password:", font=fontStyle)
    # pass_box = tk.Text(root, bg="grey", font=fontStyle)
    pass_box = tk.Entry(root, bg="grey", font=fontStyle, show="*")
    pass_label.pack()
    pass_box.pack()

    # login button
    log_button = tk.Button(root, text='Login', height=2, width=20, command=lambda:(button_pressed()))
    log_button.pack()
    user_send = user_box
    pass_send = pass_box
    root.mainloop()  # run tkinter display

def button_pressed():
    BtPressed = True

if __name__ == '__main__':
    global BtPressed
    BtPressed = False
    status = False
    my_socket.send(pickle.dumps([BtPressed]))
    if not status:
        null_state = False # checks if the user entered null data on one or both of the fields
        root = tk.Tk()
        display_login()
        if BtPressed:
            my_socket.send(pickle.dumps([user_send, pass_send]))  # Sending data to the server
            status = pickle.loads(my_socket.recv(1024))  # Getting data from the server
    # success_msg = f"Hello {logged_user}, you logged in successfully!"
    # mb.showinfo(title="login succeed!", message=success_msg)

我不知道发生了什么,我尝试了所有方法,如果可以请帮助我谢谢:)

4

0 回答 0