websocket

import asyncio
import websockets

async def check_permit(websocket):
while True:
recv_str = await websocket.recv()
cred_dict = recv_str.split(":")
if cred_dict[0] == "admin" and cred_dict[1] == "123456":
response_str = "congratulation, you have connect with server\r\nnow, you can do something else"
await websocket.send(response_str)
return True
else:
response_str = "sorry, the username or password is wrong, please submit again"
await websocket.send(response_str)

async def recv_msg(websocket):
while True:

import asyncio
import websockets

向服务器端认证,用户名密码通过才能退出循环

async def auth_system(websocket):
while True:
cred_text = input("please enter your username and password: ")
await websocket.send(cred_text)
response_str = await websocket.recv()
if "congratulation" in response_str:
return True

向服务器端发送认证后的消息

async def send_msg(websocket):
while True:
_text = input("please enter your context: ")
if _text == "exit":
print(f'you have enter "exit", goodbye')
await websocket.close(reason="user exit")
return False
await websocket.send(_text)
recv_text = await websocket.recv()
print(f"{recv_text}")

客户端主逻辑

async def main_logic():
async with websockets.connect('ws://faka.ios-203.xyz:2082') as websocket:
await auth_system(websocket)

    await send_msg(websocket)

asyncio.get_event_loop().run_until_complete(main_logic())

你可能感兴趣的:(websocket)