简体   繁体   English

Python服务器即时断开连接

[英]Python server instant disconnect

Here's the code giving me the issue: 这是给我问题的代码:

def connect():
    s.listen(2)
    print("Server listening")
    conn,addr=s.accept()
    print("Connected with " + str(addr) + '\n')
    recv()

def recv():
    while 1:
        try:
            print("Starting try statement")
            data=conn.recv(1024)
            if data == "":
                print("No data")
                recv()
            else:
                print("Data")
                print(data.decode('UTF-8') + " -END")
                recv()
        except:
            print("No connection")
            connect()
        conn.close()

When I execute the code, it'll connect to the client and be ready to receive a message at any point. 当我执行代码时,它将连接到客户端,并随时准备接收消息。 However, once it's executed this is what appears. 但是,一旦执行,就会显示出来。

Server listening
Connected with ('xx.xxx.xxx.xx', xxxxx)

Starting try statement
No connection
Server listening

IP censored. IP经过审查。 Does anyone have a fix for this? 有人对此有解决办法吗?

EDIT: Typo 编辑:错别字

CLIENT CODE (From TKinter GUI) 客户代码(来自TKinter GUI)

    s.connect((host,port))
        self.chatlog['state'] = NORMAL
        self.chatlog.insert(END, ("===CONNECTED TO SERVER\n"))
        self.chatlog['state'] = DISABLED
        self.chatlog.yview(END)
        self.conn=True
        print("Connected")

You are doing it wrong. 你做错了。

Ya 'now that local conn what you are creating in function connect is not accessible from function recv? 是的,现在您不能在函数recv中访问在函数连接中创建的本地conn吗? That is a reason for not reciving anything. 这就是不接收任何东西的原因。

My solution using that code, without using classes and threads but with select and sys module is: 我使用该代码(不使用类和线程,但使用select和sys模块)的解决方案是:

import sys
import select

def connect()
    s.listen(2)
    print('Sever listening')
    inputs = [s, sys.stdin]
    running = 1
    while running:
        i_rdy = select.select(inputs,[],[],1)[0]

        if s in i_rdy:
            conn, addr = s.accept()
            print ('Connected with ' + str(addr) + '\n')
            recv(conn)

        if sys.stdin in i_rdy:
            junk = std.stdin.readline()
            if junk.lstrip('\n') == 'exit':
                running = 0
                print('Closing server')

    s.close()

def recv(conn):
    while 1:
        try:
            print("Starting try statement")
            data = conn.recv(1024)
            if data == "":
                print("No data")

            else:
                print("Data")
                print(data.decode('UTF-8') + " -END")

        except:
            print("No connection")
            #traceback
            print(sys.exc_info)
            break
    try:
        conn.close()
    except:
        pass

As you can see can "exit" when u type exit to console but only when there is no active connection... 如您所见,当您在控制台上键入exit时,但仅当没有活动连接时,才能“退出” ...

That why you should consider rewrite this to classes, it would be a lot easier to stop, not "ugly" and it could handle multiple connections. 这就是为什么您应该考虑将此重写为类的原因,这将使停止变得容易得多,而不是“丑陋”,并且它可以处理多个连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM