zoukankan      html  css  js  c++  java
  • Python TCP 协议网络编程《五》

    今日主题:今天分享的内容是在Python TCP 协议网络编程《四》的基础上增加了在服务端的登录用户校验,如果客户端输入的用户名不是服务端列表中的,就无法进入正常的聊天室。

    具体实现代码如下

    server.py文件内容如下

     1from socket import socket,SOCK_STREAM,AF_INET #导入模块
     2from threading import Thread #导入线程包
     3
     4sockets=[] #用于存放所有请求过来的client的socket
     5
     6userlist=['tony','jack','lucy','lily']
     7
     8def tcp_server():
     9    tcp_server_socket=socket(AF_INET,SOCK_STREAM) #创建TCP服务端套接字对接,UDP是SOCK_DGRAM
    10    server_address=('127.0.0.1',9999)#定义本TCP服务端的ip and port
    11    tcp_server_socket.bind(server_address) #绑定本机的8888端口
    12    tcp_server_socket.listen(128) #设置监听,并设置最大等待连接数为128
    13    while 1:
    14        print("i'm accepting")
    15        client_socket, client_info = tcp_server_socket.accept()  # 接收客户端的socker and info
    16        print(client_socket,client_info)
    17        sockets.append(client_socket)
    18        t=Thread(target=get_msg,args=(client_socket,)) #开启线程来处理当前请求过来的client
    19        t.start()
    20
    21def get_msg(client_socket):
    22    while 1:
    23        data=client_socket.recv(1024)
    24        print("the data:",data)
    25        name=str(data,encoding="utf-8")
    26        print("the data:",name)
    27        if(len(data)>0 and name in userlist):
    28            for sk in sockets:
    29                print(sk,data)
    30                sk.send(data)
    31        else:
    32            for sk in sockets:
    33                sk.send("username is wrong,please input again...".encode("utf-8"))
    34
    35
    36if __name__ == '__main__':
    37    print("the TCP server is running ...")
    38    tcp_server()

    client.py文件内容如下

     1from socket import socket,SOCK_STREAM,AF_INET #导入模块
     2from threading import Thread #导入线程包
     3
     4tcp_client_socket = socket(AF_INET, SOCK_STREAM)  # 创建TCP客户端套接字对接
     5server_address = ('127.0.0.1', 9999)  # 定义本TCP服务端的ip and port
     6tcp_client_socket.connect(server_address)  # 连接到服务端
     7
     8response=''
     9count=1
    10
    11def tcp_client():
    12    thread1=Thread(target=read_msg,args=(tcp_client_socket,))
    13    thread1.start()
    14
    15    thread2= Thread(target=send_msg, args=(tcp_client_socket,))
    16    thread2.start()
    17
    18def send_msg(tcp_client_socket):
    19    global count
    20    while 1:
    21        if(count==1):
    22            message = input(">>>请输入你的登录名称:")
    23        else:
    24            if("wrong" not in response):
    25                message = input(">>>可以开始聊天:")
    26            else:
    27                message = input(">>>请输入你的登录名称:")
    28        tcp_client_socket.send(message.encode("utf-8"))
    29        import time
    30        time.sleep(1)
    31        count+=1
    32
    33def read_msg(tcp_client_socket):
    34    global response
    35    while 1:
    36        resv_data = tcp_client_socket.recv(1024)
    37        print("the response of message is:",resv_data.decode("utf-8"))
    38        response=resv_data.decode("utf-8")
    39
    40if __name__ == '__main__':
    41    print("the TCP client is running ...")
    42    tcp_client()

    client console output内容如下

    1the TCP client is running ...
    2>>>请输入你的登录名称:aa
    3the response of message is: username is wrong,please input again...
    4>>>请输入你的登录名称:bb
    5the response of message is: username is wrong,please input again...
    6>>>请输入你的登录名称:tony
    7the response of message is: tony
    8>>>可以开始聊天:

    欢迎关注【无量测试之道】公众号,回复【领取资源】,
    Python编程学习资源干货、
    Python+Appium框架APP的UI自动化、
    Python+Selenium框架Web的UI自动化、
    Python+Unittest框架API自动化、

    资源和代码 免费送啦~
    文章下方有公众号二维码,可直接微信扫一扫关注即可。

    备注:我的个人公众号已正式开通,致力于测试技术的分享,包含:大数据测试、功能测试,测试开发,API接口自动化、测试运维、UI自动化测试等,微信搜索公众号:“无量测试之道”,或扫描下方二维码:

     添加关注,让我们一起共同成长!

  • 相关阅读:
    poj----1330Nearest Common Ancestors(简单LCA)
    Tarjan--LCA算法的个人理解即模板
    hdu----(4545)魔法串(LCS)
    hdu---(1325)Is It A Tree?(并查集)
    hdu----(1599)最大子矩阵(几何/dp)
    hdu---(1054)Strategic Game(最小覆盖边)
    整理的一些模版LCS(连续和非连续)
    hdu---(4310)Hero(贪心算法)
    hdu----(4308)Saving Princess claire_(搜索)
    hdu------(4302)Holedox Eating(树状数组+二分)
  • 原文地址:https://www.cnblogs.com/Wu13241454771/p/13906989.html
Copyright © 2011-2022 走看看