Python套接字+;选择
我使用此功能通过套接字运行服务器:Python套接字+;选择,python,Python,我使用此功能通过套接字运行服务器: def run(self): # The 'main' function print 'Running ... ' Running = True while Running: InList,OutList,ExceptList = select.select(self.Connections,[],[]) for Connection in InList: if Conne
def run(self):
# The 'main' function
print 'Running ... '
Running = True
while Running:
InList,OutList,ExceptList = select.select(self.Connections,[],[])
for Connection in InList:
if Connection == self.Server:
# Server got a new connecting Client
User, Adress = self.Server.accept() # New User Connection
Data = {'User':User,'Adress':Adress}
self.Connections.append(Data) # Store the new User Connection
print 'User ', Data, ' connected'
else:
# Some other Socket got data for the Server
Data = Connection.recv(1024)
if not Data:
print 'No new Data!'
break
print Data
但是,当发送数据时,我得到了以下错误:TypeError:参数必须是int,或者在第23行select()行上有一个fileno()方法
查阅手册和那些例子,我看不出有什么不同,也不明白我为什么不工作。self.Connections仅包含服务器套接字,当使用print self.Connections时,它会为我提供:
[<socket._socketobject object at 0x020B6BC8>]
[]
声明,这是我传递给select()的一个列表,应该是正确的
我做错了什么?谢谢 第一次运行
select.select
时,没有问题,因为self.Connections
只包含一个套接字对象,它是完全有效的
但是,在通过
while
循环的第二次行程中,self.Connections
拾取了另一个元素:在if Connection==self.Server:
块中构造的数据
字典。该字典不是整数,并且没有fileno
方法,因此select.select
在看到它时会抱怨。只是一个提示-Python的惯例是变量是小写的。