Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当客户端断开连接时,为什么我的服务器打印\n?_Python_Sockets - Fatal编程技术网

Python 当客户端断开连接时,为什么我的服务器打印\n?

Python 当客户端断开连接时,为什么我的服务器打印\n?,python,sockets,Python,Sockets,我在创建一个服务器的过程中,但我撞到了墙,似乎无法爬过去 在用户断开与服务器的连接之前,我的当前代码工作得非常好。大量的\n被打印出来,我似乎不明白为什么 我知道问题在于recv函数。我相信这与我的线程有关,但我似乎不知道它是什么 这是密码,如果有人能帮我,我将非常感激 def recv(self, obj, addr, s): while True: try:print obj.recv(1024) except:pass def connect(se

我在创建一个服务器的过程中,但我撞到了墙,似乎无法爬过去

在用户断开与服务器的连接之前,我的当前代码工作得非常好。大量的\n被打印出来,我似乎不明白为什么

我知道问题在于recv函数。我相信这与我的线程有关,但我似乎不知道它是什么

这是密码,如果有人能帮我,我将非常感激

def recv(self, obj, addr, s):
    while True:
        try:print obj.recv(1024)
        except:pass

def connect(self, port):
    s = socket.socket()
    s.bind(('', port))
    s.listen(1)
    self.clearscreen()

    print "Waiting for connections..."
    while True:
        obj, addr = s.accept()
        verify = obj.recv(1024)
        if verify == "ea25364e2dab91b40ae4f73163854b5d":
            print "\n"+str(addr) + " has connected.\n "
            self.conns[addr] = obj
            try:
                Thread(target=self.handle, args=(obj, addr, s)).start()
                Thread(target=self.recv, args=(obj, addr, s)).start()
            except:pass
        else:pass

因为您在呼叫recv时从未检查EOS,所以您从未检测到断开连接。

好的,看起来我已经解决了

  def recv(self, obj, addr, s):
      while True:
          try:a = obj.recv(1024)
          except:pass
          if not p: return 1
          else: print a

好的,但问题是我已经在检查错误了。我不明白如何解决这个问题。