我是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
这是我的剧本:
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
您认为上述脚本有什么问题?每次出现错误时我都必须手动重启我的脚本,这对我来说很沮丧。
更新
我尝试了@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()
我认为它解决了错误,但后来我得到了这个:
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>
更新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]
我仍然会收到此错误:
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
答案 0 :(得分:0)
在尝试删除或仅使用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