使用python的多客户端服务器

2024-09-30 12:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我通过多线程模块编写了一个客户机和服务器程序,但代码有问题

考虑以下输出:(客户端超过2(这是一个例子))

+---------+--------------+--------+
| [Index] | [IP-Address] | [Port] |
+---------+--------------+--------+
|    0    | 192.168.1.12 | 34224  |
|    1    | 192.168.1.10 | 39870  |
+---------+--------------+--------+

我希望服务器在用户输入“selected[number]”命令时向客户端发送相应的命令(如selected 0 1)

如果用户发送了错误的号码,例如:

chose 2 3
chose 0 2
chose 2 0
chose
chose 0 (just one number)

服务器显示无效的选择

我的输出:

+---------+--------------+--------+
| [Index] | [IP-Address] | [Port] |
+---------+--------------+--------+
|    0    | 192.168.1.12 | 34232  |
|    1    | 192.168.1.10 | 40032  |
+---------+--------------+--------+
[BAT]> chose    
[ERROR] Chose more than one!
[BAT]> chose 2 3
[WARNING] Not valid selection
[WARNING] Not valid selection
[BAT]> chose 
[ERROR] Chose more than one!
[BAT]> chose 0  
[ERROR] Chose more than one!
[BAT]> chose 0 1
[Multicasting-CMD]> mkdir test2
[STATUS] Success
[Multicasting-CMD]> 
[WARNING] Command is empty!
[BAT]> 
[WARNING] Command is empty!

我的代码是:

if "chose" in COMMAND:
    Multicasting_check()


def Multicasting_check(COMMAND):
    Target_list = COMMAND.replace("chose ", "")
    Target_list = Target_list.split()

    checking_list = []

    if len(Target_list) > 1:

        for conn_item in ALL_CONNECTIONS:
            checking_list.append(ALL_CONNECTIONS.index(conn_item))

        for Items in Target_list:
            Int_items = int(Items)

            if Int_items in checking_list:
                Multicasting(Int_items)
            else:
                print "[WARNING] Not valid selection"
                continue
    else:
        print "[ERROR] Chose more than one!"




def Multicasting(Conn_number):
    Message = raw_input("[Multicasting-CMD]> ")

    if len(Message) > 0:
        if Message == "quit":
            BAT()

        try:
            ALL_CONNECTIONS[Conn_number].send(str.encode(Message))
            print colored("[STATUS]", "blue"), colored("Success", "green")
        except socket.error:
            print "[ERROR] Connection was lost with", ALL_HOST_ADDRESSES[Conn_number][0]
    else:
        print "[WARNING] Command is empty!"

所有客户端都无法执行这些命令 我怎样才能修好它

谢谢各位


Tags: innumbertargetifmoreerroronelist

热门问题