0

この関数を使用して、ソケット経由でサーバーを実行します。

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 である必要があります。または、select() 行である 23 行目に fileno() メソッドが必要です。

マニュアルとそれらの例を調べる ( http://code.activestate.com/recipes/531824-chat-server-client-using-selectselect/およびhttp://ilab.cs.byu.edu/python/select/echoserver .html ) 違いが見られず、なぜ機能しないのか理解できません。self.Connections には Server ソケットのみが含まれており、print self.Connections を使用すると、次のようになります。

[<socket._socketobject object at 0x020B6BC8>]

それは正しいはずのselect()に渡すリストだと述べています。

私は何を間違っていますか?ありがとう!

4

1 に答える 1

1

初めて実行するときは、完全に有効なソケット オブジェクトのみが含まれているselect.selectため、問題はありません。self.Connections

whileただし、ループの 2 回目のトリップでは、別の要素、つまりブロック内で構築された辞書self.Connectionsが取り上げられています。その辞書は整数ではなく、メソッドを持っていないため、それを見ると文句を言います。Dataif Connection == self.Server:filenoselect.select

于 2013-11-12T19:18:37.313 に答える