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:
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,
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
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
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.