简体   繁体   English

Python套接字网络-ValueError:list.remove(x):x不在列表中

[英]Python socket networking - ValueError: list.remove(x): x not in list

I'm new in python so please bear with me, I have a script basically about socket networking, sometimes I get the following error when running the script: 我是python的新手,请耐心等待,我基本上有一个有关套接字网络的脚本,有时在运行该脚本时遇到以下错误:

Traceback (most recent call last):
  File "socks", line 293, in <module>
    server.main_loop()
  File "socks", line 210, in main_loop
    self.on_close()
  File "socks", line 237, in on_close
    self.input_list.remove(self.s)
ValueError: list.remove(x): x not in list

Here's the script that I have: 这是我拥有的脚本:

class TheServer:
    input_list = []
    channel = {}
    channel_ = {}
    request = {}

    def __init__(self, host, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((host, port))
        self.server.listen(200)

    def main_loop(self):
        self.input_list.append(self.server)
        while 1:
            ss = select.select
            inputready, outputready, exceptready = ss(self.input_list, [], [])
            for self.s in inputready:
                if self.s == self.server:
                    self.on_accept()
                    break
                try:
                    self.netdata = self.s.recv(buffer_size)
                except Exception, e:
                    self.netdata =''
                if len(self.netdata) == 0:
                    self.on_close()
                else:
                    if cmp(self.channel[self.s],self.channel_[self.s]):
                        self.on_outbounddata()
                    else:
                        self.on_execute()

    def on_accept(self):
        forward = Forward().start(forward_to[0], forward_to[1])
        clientsock, clientaddr = self.server.accept()
        if forward:
            self.input_list.append(clientsock)
            self.input_list.append(forward)
            self.channel[clientsock] = forward
            self.channel[forward] = clientsock
            self.channel_[clientsock] = forward
            self.channel_[forward] = forward
        else:
            waktu = datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S')
            with open('/www/dial.log', 'a') as file:
              file.write(str(waktu) + " Proxy " + str(forward_to[0]) + ":" + str(forward_to[1]) + " isn't responding\n")
              file.close()
            print "Proxy " + str(forward_to[0]) + ":" + str(forward_to[1]) + " isn't responding"
            print "Closing connection with client side", clientaddr
            clientsock.close()

    def on_close(self):
        self.input_list.remove(self.s)
        self.input_list.remove(self.channel[self.s])
        out = self.channel[self.s]
        self.channel[out].close()  
        self.channel_[out].close() 
        self.channel[self.s].close()
        self.channel_[self.s].close()
        del self.channel[out]
        del self.channel_[out]
        del self.channel[self.s]
        del self.channel_[self.s]



    def on_execute(self):
        netdata = self.netdata

What do you think is wrong with the script above ? 您认为上述脚本有什么问题? I have to manually restart my script every time I get that error, it's kind of frustrating for me. 每当遇到该错误时,我都必须手动重新启动脚本,这让我感到沮丧。

Update: 更新:

I tried the suggestion from @Moses, 我尝试了@Moses的建议,

def on_close(self):
    try:
      self.input_list.remove(self.s)
    except ValueError:
      print "self.s is not in the list"
    self.input_list.remove(self.channel[self.s])
    out = self.channel[self.s]
    self.channel[out].close()  
    self.channel_[out].close() 
    self.channel[self.s].close()
    self.channel_[self.s].close()

I thought it solved the error but then I got this: 我以为它解决了错误,但是后来我明白了:

Traceback (most recent call last):
  File "socks", line 296, in <module>
    server.main_loop()
  File "socks", line 210, in main_loop
    self.on_close()
  File "socks", line 241, in on_close
    self.input_list.remove(self.channel[self.s])
KeyError: <socket._socketobject object at 0x7f0669e93a60>

Update 2: I modified my script to be like this 更新2:我修改了脚本,就像这样

def on_close(self):
    try:
      self.input_list.remove(self.s)
    except ValueError:
      print "self.s is not in the list"
      pass
    self.input_list.remove(self.channel.get(self.s, None))
    out = self.channel[self.s]
    self.channel[out].close()  
    self.channel_[out].close() 
    self.channel[self.s].close()
    self.channel_[self.s].close()
    del self.channel[out]
    del self.channel_[out]
    del self.channel[self.s]
    del self.channel_[self.s]

And I still get this error: 而且我仍然收到此错误:

self.s is not in the list
Traceback (most recent call last):
  File "socks", line 296, in <module>
    try:
  File "socks", line 210, in main_loop
    self.on_close()
  File "socks", line 241, in on_close
    pass
ValueError: list.remove(x): x not in list

Check if the item you have exists in the list before attempting to remove or just use a try-except 在尝试删除或仅使用try-except之前,请检查列表中是否存在您所拥有的项目

if item in lst:
    lst.remove(item)
else:
    pass # you may print some message or log something

# OR

try:
    lst.remove(item)
except ValueError:
    pass # you may print some message or log something

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

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