Georgey added the comment:

"Without code or something demonstrating the bug, I’m pretty sure it is a bug 
in your program"

Here is the main Thread

mailbox = queue.Queue()

while True:

    for key in yellow_page.keys():
        if yellow_page[key][0] ==None:

    print("\n", name_groups)
    if len(unknown_clients) >0:
        print("unknown from:"+str(unknown_clients))
    print(time.strftime(ISOTIMEFORMAT, time.localtime(time.time())) + '\n')

    # Get the list sockets which are ready to be read through select
    read_sockets, write_sockets, error_sockets =,[],[],TIMEOUT)

    for sock in read_sockets:
        #New connection
        if sock ==server_sock:
            # New Client coming in
            clisock, addr = server_sock.accept()  
            ip = addr[0]
            if ip in IPALLOWED:
                yellow_page[addr] = [None,None,clisock] 
        #Some incoming message from a client
            # Data recieved from client, process it
                data = sock.recv(BUFSIZ)
                if data:
                    fromwhere = sock.getpeername()
                    mail_s = data.split(SEG_) 
                    del mail_s[0]
                    for mail_ in mail_s:

                        mail = mail_.decode()                        
                mailbox.put( (("sock_err",sock), 'localhost') )

so the sub thread's job is to analyze the exception put into "mailbox"

Here is the run function of sub thread
    def run(self):
        while True:
            msg, addr = mailbox.get()  
            if msg[0] =="sock_err":
                print("sock_err @ ", msg[1])  #<<<Here comes the print of 
socket object
                continue ##jump off
            else: ......

Let us see how the handle_sock_err does to the broken socket:

def handle_sock_err(sock): #sock是出错的网络连接,要注销它并且提示出错
    global active_socks, yellow_page, addr_groups, name_groups 
    addr_del = sock.getpeername()  #<<<ERROR 10038
    name_del, job_del = yellow_page[addr_del][ 0:2] 
    tag = 0

        addr_groups[job_del].remove(addr_del);   tag =1
        name_groups[job_del].remove(name_del);   tag =2
        tag =3

        print(name_del+" offline!")

        if tag <3:


I do believe that the broken socket can tell me the address it connected to, so 
there is even no "try" in getpeername()

Why do I need to find the address of that broken socket found by select in main?
Simple, the server recognizes the user name once the connection has sent 
correct login information. When the connection is down, the user shall be 
automatically removed from online user list "yellow_page" and all other dynamic 
books like "addr_groups", "name_groups"...

This is a very common and reasonable practice of online system. I am not 
particularly interested in why getpeername() is ineffective in getting the 
address stopped connection,

but How I get the address that stopped connection.

I do not know why python can only tell me a line has broke, but where it was 
leading to. And I believe this is a big issue in establishing an effective 
server, do you agree with me?

resolution: not a bug -> remind
status: closed -> open

Python tracker <>
Python-bugs-list mailing list

Reply via email to