为什么在下面的代码段中会出现错误listed is not defined
import socket,select
from threading import *
import time
neighbours=[]
def neighbourfuncall():
print('In neighbours')
class pyserver(Thread):
dictn={}
HOST=socket.gethostname()
PORT=8888
buf=1024
ADDR=(HOST,PORT)
listed=[]
sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
sock.bind(ADDR)
sock.listen(30)
def __init__(self):
self.interval=6
listed.append(sock)
thread=threading.Thread(target=neighbourfuncall,args=())
thread.daemon=True
thread.start()
def run(self):
while True:
sel,out,spl=select.select(listed,[],[],15.0)
for s in sel:
if s==sock:
client,address=sock.accept()
listed.append(client)
dest=client.recv(buf)
dictn[client]=dest
else:
pass
serv=pyserver()
serv.run()
在类中,需要使用
self.list.append(smth)
。必须使用self
访问所有类变量顺便说一下,套接字操作必须在
__init__()
中。你最好这样做:必须使用以下语法访问
listed
:因为它是一个静态类变量
相关问题 更多 >
编程相关推荐