Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python“==&引用;在应该返回True时返回False_Python_Sockets - Fatal编程技术网

Python“==&引用;在应该返回True时返回False

Python“==&引用;在应该返回True时返回False,python,sockets,Python,Sockets,我最近在python中尝试使用密码保护套接字,但遇到了一个问题 当使用服务器设置的密码检查客户端输入时,服务器似乎认为设置的密码与用户输入的密码不同 我的第一个脚本:server.py import socket import threading from requests import get import uuid HEADER = 64 PORT = 9090 #To connect over the internet change SERVER to your public IP SER

我最近在python中尝试使用密码保护套接字,但遇到了一个问题

当使用服务器设置的密码检查客户端输入时,服务器似乎认为设置的密码与用户输入的密码不同

我的第一个脚本:server.py

import socket
import threading
from requests import get
import uuid
HEADER = 64
PORT = 9090
#To connect over the internet change SERVER to your public IP
SERVER = socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)

def handle_client(conn, addr, password):
    print(f"[PNet] NEW CONNECTION: {addr} connected.")

    connected = True
    while connected:
        msg_length = conn.recv(HEADER).decode(FORMAT)
        if msg_length:
            msg_length = int(msg_length)
            msg = conn.recv(msg_length).decode(FORMAT)
            print(msg)
            if msg == password:
                connected = True
            if msg != password:
                print("Huh?")
            if msg == DISCONNECT_MESSAGE:
                connected = False
            print(f"[PNet] {addr}: {msg}")
            conn.send("[PNet] CLIENT: Message received.".encode(FORMAT))
    conn.close()
    
def start():
    print("[PNet] STARTING: Server is starting...")
    print("[PNet] STARTING: Generating key...")
    password = uuid.uuid4()
    print(f"[PNet] FINALIZING: Key generated: {password}")
    server.listen()
    print(f"[PNet] LISTENING: Server is listening on {SERVER}.")
    while True:
        conn, addr = server.accept()
        thread = threading.Thread(target=handle_client, args=(conn, addr, password))
        thread.start()
        #print(f"[PNet] ACTIVE CONNECTIONS: {threading.activeCount() - 1}")
        
start()
还有我的第二个脚本:client.py

import socket

HEADER = 64
PORT = 9090
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = "!DISCONNECT"
SERVER = input("[PNet] CLIENT: Server IP: ")
PASS = input("[PNet] SERVER: Enter password: ")
ADDR = (SERVER, PORT)

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)

def send(msg):
    message = msg.encode(FORMAT)
    msg_length = len(message)
    send_length = str(msg_length).encode(FORMAT)
    send_length += b' ' * (HEADER - len(send_length))
    client.send(send_length)
    client.send(message)
    print(client.recv(2048).decode(FORMAT))

send(PASS)

connected = True
while connected:
    msg = input("[PNet] CLIENT: Send a message: ")
    if msg != DISCONNECT_MESSAGE:
        send(msg)
    else:
        send(msg)
        connected = False

当运行时,密码完全从终端复制,它仍然返回False。感谢您的帮助:)

您正在用字符串映射UUID对象,这就是它返回false的原因

要进行检查,请在比较之前打印

...
print(type(msg),type(password)) # will print <class 'str'> <class 'uuid.UUID'>

if msg == password:
    connected = True
...

请注意,
socket.recv(n)
不保证返回
n
字节。你可能需要反复阅读,直到读够为止。
password = str(uuid.uuid4())