1

我对此很陌生。我正在尝试构建一个服务器(聊天服务器)抱歉提供了这样一个混乱的代码。关于这段代码,我要改变很多事情。但到目前为止,我只需要一件事的帮助:

当我开始让我们对此多说一个……然后关闭客户端时,我收到以下消息:

线程中未处理的异常由

正如您在这段代码的许多地方看到的那样,我试图杀死线程。但我不知道我做错了什么..我是新手。关于我应该做什么的任何建议?

#encoding: utf-8

import socket, random, time, thread, errno, traceback

print socket.gethostname()
print "current machines IP address: "+socket.gethostbyname(socket.gethostname())

host ="10.0.0.1"# raw_input("IP: ")
# = socket.gethostbyname(socket.gethostname())
port = 12345

print host

conn_list =[None]*10
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((host, port))
sock.listen(10)

print "connected..\n\n\n\n"

def recv(conn):
    while True:
        try:
            message = conn.recv(1024)
            if "MESG" == message[1:5]:
                message = message[6:].split(':')
                name = str(conn)
                conn_number = conn_list.index(conn)
                conn_name = str(conn_number)
                message = message[2]
                reciever = message[0:1]
                reciever = int(reciever)
                for conn in conn_list:
                    if reciever == conn_list.index(conn):
                        conn.send(message)
                        print "Connection   "+conn_name+"  ----->  "+str(reciever)+"   :"+message+"\n"
                        #conn = findTheRightConnection(conn_list, conn_number)
                        break
                    else:
                        pass

        except ValueError:
            print "ValueError by %s" % (str(conn))
            print conn.send("\nOpps you are not typing the correct connection number infront  your message!")

        except IOError:
            bye(conn,conn_list)
            print"Going to try to kill the thread here "
            thread.quit()
            thread.isAlive()
            print "Still alive..."

        except socket.error, v:
            errorcode=v[0]
            bye(conn,conn_list)
            print"Going to try to kill the thread or here"
            thread.quit()
            thread.isAlive()
            print "Still alive..."


        except Exception, e:
            traceback.print_exc()

        finally:
            thread.isAlive()
            print"\nChanging the conn back to what it was... "
            conn = findTheRightConnection(conn_list, conn_number)


def handle_connection(conn):
    try:
        recv(conn)

    except socket.error, v:
        errorcode=v[104]
        bye(conn)


def bye(conn,conn_list):
    i= 0
    print "bye"
    connectionName = str(conn_list.index(conn))
    conn.close
    conn_list = conn_list
    print conn_list
    for element in conn_list:
        if element == conn:
            conn_list[i] = None
            break
        i += i
        print "Connection number "+connectionName+" is terminated"
        print conn_list

        return "Connection Terminated"


def welcome(conn,conn_list):
    i = 0
    for element in conn_list:
        if element == None:
            conn_list[i] = conn
            print "Connection added in the conn_list on the slot %d" % (i)
            print conn_list
            return conn_list

        else:
            i = i+1
            pass
    print "The server if full! No more space left"
    return conn_list


def findTheRightConnection(conn_list, number):
    for conn in conn_list:
        if number == conn_list.index(conn):
            return conn
        else:
            pass

        print "\nSomthing went wrong while trying to find the right connection in the method findTheRightConnection()"
        return


while True:
    conn, addr = sock.accept()
    conn_list = welcome(conn,conn_list)
    print "Got connection from : "+str(addr[0])+" by connection number: "+str(conn_list.index(conn))+"\n\n\n\n"     
    msg = "Welcome to the server!"
    conn.send(":INFO:"+str(int(time.time()))+":"+str(len(msg))+":"+msg)
    thread.start_new_thread(handle_connection, (conn,))
4

1 回答 1

0

如果您仍然无法在 Python 中创建即时消息程序,您可能会对另一个问题的答案感兴趣。

  • Simple_Server.py是一个最小的服务器实现。可以根据要求提供具有多种功能的更复杂的服务器。复杂的服务器支持身份验证、朋友、私人消息传递、频道、过滤器、数学评估和管理控制。
  • MultichatClient.py是网络课老师编写的 Java 程序的移植版。该程序必须从命令行运行,并且必须将其作为参数提供给服务器。您可以使用网络上的服务器名称或其 IP 地址。
  • Simple_Client.pyw是一个更复杂的客户端,不需要从命令行启动。当它启动时,它会询问服务器的名称并尝试连接它,同时显示一个进度对话框。该程序将自动尝试将任何错误记录到文件中。
  • 要运行threadbox.py , affinity.py是必需的。(无论来源如何,在特定线程上运行代码)
  • 运行safetkinter.py需要threadbox.py。(元类克隆类以使用亲和力运行)
  • Simple_Client.pyw需要safetkinter.py才能运行。(使 tkinter 可以安全地与线程一起使用)
于 2014-09-05T15:42:01.677 回答