本文整理汇总了Python中messenger.Messenger.broadcast方法的典型用法代码示例。如果您正苦于以下问题:Python Messenger.broadcast方法的具体用法?Python Messenger.broadcast怎么用?Python Messenger.broadcast使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类messenger.Messenger
的用法示例。
在下文中一共展示了Messenger.broadcast方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: thread_client
# 需要导入模块: from messenger import Messenger [as 别名]
# 或者: from messenger.Messenger import broadcast [as 别名]
def thread_client(conn, addr, db_conn, active_clients):
"""
Checks and verifies password/username, and handles adding new
users to the database.
Main client loop Accepts messages from client socket then
broadcasts message to all clients. If the connection is broken
the loop will break, database will be updated(active state).
:param conn: socket objected connected with remote client
:param addr: tuple of the remote clients address and port
:param db_conn: connection to the sqlite3 database containing user-info
"""
length_struct = struct.Struct("!I")
local_messenger = Messenger(conn, length_struct)
lock = _thread.allocate_lock()
verified = False # used to control looping
while not verified: # handle client login/signup credentials
try:
"""
first message received will be a login or sign up attempt
message_type will be "LOGIN" or "SIGNUP"
"""
message = local_messenger.recv()
message_type, username, password = message.split("`", 2)
except ValueError or ConnectionResetError:
print("bad connection at {}".format(addr))
break
# retrieve user info from database. username_check() returns two boolean values
lock.acquire()
username_exists, correct_password = username_check(db_conn, username, password)
lock.release()
# add new users to database
if message_type == "SIGNUP":
if username_exists: # username already taken
local_messenger.send("UNAVAILABLE")
else:
# acquire lock and add user to database and active_clients
lock.acquire()
new_user(db_conn, username, password)
active_clients[username] = conn
lock.release()
local_messenger.send("OK")
verified = True
# login existing users
elif message_type == "LOGIN":
if username_exists and correct_password:
if username not in active_clients: # username is not already signed in
# acquire lock and add username to active_clients
lock.acquire()
active_clients[username] = conn
lock.release()
local_messenger.send("OK")
verified = True
else:
local_messenger.send("USER_ACTIVE") # user is already active
else:
local_messenger.send("BAD") # wrong password or username
while verified:
"""
client will only be verified when an existing username and password have been
submitted, or a new username and password has been created.
verified loop will handle all incoming messages, errors, socket closures
"""
try:
message = local_messenger.recv()
except socket.error or struct.error:
print("bad connection at {}".format(addr))
break
if message:
lock.acquire()
local_messenger.broadcast(active_clients, message)
lock.release()
else:
# empty string signaling connection closed
lock.acquire()
del active_clients[username]
lock.release()
conn.close()
break
# clean up after client disconnects or the connection is broken
if username in active_clients:
lock.acquire()
del active_clients[username]
lock.release()
conn.close()
print("{} DISCONNECTED".format(addr))