我的服务器上的套接字有什么问题?



这是我的代码

import sqlite3
import socket
import re
import face_recognition
import os, sys
import cv2
import numpy as np
import math
from recognition import FaceRecognizer
from SalaryDemo import Salary
from Camera import SignUpCamera

def main():
# Connect to the SQLite database
conn = sqlite3.connect('users.db')
cursor = conn.cursor()
cursor.execute("PRAGMA table_info('users')")
result = cursor.fetchone()
if not result:
# If the 'users' table does not exist, create it
cursor.execute('''CREATE TABLE users (id INTEGER PRIMARY KEY,
username TEXT,
password TEXT)''')
cursor.execute("ALTER TABLE users ADD COLUMN user_data TEXT")


# Set up the server
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('localhost', 8080))
server.listen()
open = True
# Wait for incoming connections
client, addr = server.accept()
print(f'Connected by {addr}')
while True:

# Receive login/sign-up data from the client
data = client.recv(1024)
action, username, password = data.decode().split(':')

if action == 'login':
recognizer = FaceRecognizer()
recognizer.load_faces("faces")
if recognizer.recognize_face(username):
cursor.execute('SELECT * FROM users WHERE username = ? AND password = ?', (username, password))
result = cursor.fetchone()
else:
print(f"{username} not matched!")
result = None

if result:
# If the login is successful, send the user's data back to the client
user_data = f'{result[0]}:{result[1]}:{result[2]}'
msg_to_client = "welcome " + username
client.send(msg_to_client.encode())
sl = Salary()
sl.getSallary(username)                
else:
# If the login is unsuccessful, send an error message back to the client
client.send('Invalid login'.encode())

elif action == 'signup':
cm = SignUpCamera()
# Check if the password is strong enough
if not re.search(r'[A-Z]', password) or not re.search(r'[a-z]', password) or not re.search(r'[0-9]', password):
client.send('Password is not strong enough'.encode())
else:
# Check if the username is already in use
cursor.execute('SELECT * FROM users WHERE username = ?', (username,))
result = cursor.fetchone()
if result:
# If the username is already in use, send an error message back to the client
client.send('Username already in use'.encode())
else:
# If the username is available, insert a new row into the 'users' table
cursor.execute("INSERT INTO users (username, password, user_data) VALUES (?, ?, ? || '.jpg')", (username, password, username.lower()))
conn.commit()
cm.main(username)
client.send('Sign-up successful'.encode())

client.close()

if __name__ == "__main__":
main()

它可以运行但是每次我在它完成它的任务后运行它我就会得到这个错误

Message=[WinError 10038] An operation was attempted on something that is not a socket
Source=C:UsersBar12sourcereposfinal-project-afek-and-liam-authentication_client_serverServerSideProjectServerSideProjectServerSideProject.py
StackTrace:
File "C:UsersBar12sourcereposfinal-project-afek-and-liam-authentication_client_serverServerSideProjectServerSideProjectServerSideProject.py", line 89, in <module>
main()

请帮助我,我对这个有点陌生

代码应该等待一个新用户尝试登录,但是我在用户加入

后得到这个错误

你的代码中有问题的部分归结为:

while True:

# Receive login/sign-up data from the client
data = client.recv(1024)
... 
client.close()

从套接字读取,(稍后写入套接字),然后关闭套接字。然后尝试从已经关闭的中读取套接字再次出现-导致此错误。

不清楚client.close()在这部分代码中的实际用途,但它显然导致了您看到的问题。

最新更新