防止出现依赖flask应用注册的情况
This commit is contained in:
parent
0cd6c87c89
commit
2355c3845d
2
CC3.1.py
2
CC3.1.py
@ -98,7 +98,7 @@ class ChatClient:
|
||||
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
||||
sock.settimeout(5)
|
||||
try:
|
||||
sock.connect(("localhost", 8888))
|
||||
sock.connect(("localhost", 8889))
|
||||
sock.settimeout(10)
|
||||
|
||||
sock.sendall(json.dumps({
|
||||
|
15
CC3.2.py
Normal file
15
CC3.2.py
Normal file
@ -0,0 +1,15 @@
|
||||
import socket
|
||||
import json
|
||||
s = socket.socket()
|
||||
s.connect(("localhost", 8889))
|
||||
print("connected")
|
||||
s.sendall(json.dumps({
|
||||
"type": "register",
|
||||
"username": 1145,
|
||||
"password": 514
|
||||
}).encode())
|
||||
print("sended")
|
||||
response = s.recv(1024)
|
||||
print("rcved")
|
||||
result = json.loads(response.decode())
|
||||
print (result)
|
48
CS3.1.py
48
CS3.1.py
@ -3,11 +3,12 @@ import json
|
||||
from flask import Flask, jsonify, request
|
||||
import sqlite3
|
||||
import socket
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
socket_server = socket.socket()
|
||||
socket_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
||||
socket_server.bind(("localhost", 8888))
|
||||
|
||||
active_users = {}
|
||||
chat_connections = []
|
||||
|
||||
@ -41,42 +42,35 @@ def ispsswdright(name,passwd):
|
||||
else:
|
||||
return False
|
||||
|
||||
@app.route("/api/register", methods=['POST'])
|
||||
def register(usr = None,pwd = None):
|
||||
def register_user(usr, pwd):
|
||||
conn = get_db_connection()
|
||||
vl = request.get_json()
|
||||
if usr == None and pwd == None:
|
||||
usr = vl.get('username')
|
||||
pwd = vl.get('password')
|
||||
|
||||
csr2 = conn.cursor()
|
||||
csr2.execute('SELECT * FROM users WHERE name = ?', (usr,))
|
||||
result = csr2.fetchone()
|
||||
if result is not None:
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": "Username already exists"
|
||||
}), 403
|
||||
return {"success": False, "message": "Username already exists"}
|
||||
else:
|
||||
try:
|
||||
conn = get_db_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT INTO users (name, passwd) VALUES (?, ?)", (usr, pwd))
|
||||
conn.commit()
|
||||
return {"success": True, "message": "User registered successfully"}
|
||||
except sqlite3.Error as e:
|
||||
print (e)
|
||||
return jsonify({
|
||||
"success": False,
|
||||
"message": str(e)
|
||||
}), 500
|
||||
else:
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"message": "User registered successfully"
|
||||
})
|
||||
return {"success": False, "message": str(e)}
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
@app.route("/api/register", methods=['POST'])
|
||||
def register1():
|
||||
vl = request.get_json()
|
||||
usr = vl.get('username')
|
||||
pwd = vl.get('password')
|
||||
result = register_user(usr, pwd)
|
||||
if result['success']:
|
||||
return jsonify(result)
|
||||
else:
|
||||
return jsonify(result), 403 if result['message'] == "Username already exists" else 500
|
||||
|
||||
def broadcast_message(message, sender=None):
|
||||
for conn in chat_connections:
|
||||
try:
|
||||
@ -88,7 +82,8 @@ def handle_socket_message(data, addr, conn):
|
||||
try:
|
||||
action = data.get('type')
|
||||
if action == 'register':
|
||||
return register(data.get('username'), data.get('password'))
|
||||
result = register_user(data.get('username'), data.get('password'))
|
||||
return {"status": "success" if result['success'] else "error", "message": result['message']}
|
||||
elif action == 'login':
|
||||
if isuserxist(data['username']):
|
||||
if ispsswdright(data['username'], data['password']):
|
||||
@ -110,8 +105,9 @@ def handle_socket_message(data, addr, conn):
|
||||
return {"status": "error", "message": str(e)}
|
||||
|
||||
def run_socket_server():
|
||||
socket_server.bind(("localhost", 8889))
|
||||
socket_server.listen()
|
||||
print("Socket server running on port 8888")
|
||||
print("Socket server running on port 8889")
|
||||
while True:
|
||||
conn, addr = socket_server.accept()
|
||||
print(f"Socket client connected: {addr}")
|
||||
@ -141,8 +137,8 @@ def run_socket_server():
|
||||
conn.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
threading.Thread(target=run_socket_server, daemon=True).start()
|
||||
with get_db_connection() as conn:
|
||||
conn.execute('''CREATE TABLE IF NOT EXISTS users
|
||||
(name TEXT, passwd TEXT)''')
|
||||
threading.Thread(target=run_socket_server).start()
|
||||
app.run(port=5001)
|
Loading…
x
Reference in New Issue
Block a user