11

Windowsで次のpythonサーバーを実行しようとしています:

"""
An echo server that uses select to handle multiple clients at a time.
Entering any line of input at the terminal will exit the server.
"""

import select
import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
server.listen(backlog)
input = [server,sys.stdin]
running = 1
while running:
    inputready,outputready,exceptready = select.select(input,[],[])

    for s in inputready:

        if s == server:
            # handle the server socket
            client, address = server.accept()
            input.append(client)

        elif s == sys.stdin:
            # handle standard input
            junk = sys.stdin.readline()
            running = 0

        else:
            # handle all other sockets
            data = s.recv(size)
            if data:
                s.send(data)
            else:
                s.close()
                input.remove(s)
server.close() 

エラー メッセージ (10038、「ソケットではないものに対して操作が試行されました」) が表示されます。これはおそらく、「Windows のファイル オブジェクトは受け入れられませんが、ソケットは受け入れられます。Windows では、基になる select() 関数は WinSock ライブラリによって提供され、ファイル記述子を処理しません」という python ドキュメントの発言に関連しています。 t は WinSock に由来します。". インターネット上には、このトピックに関する投稿がかなりありますが、私には技術的すぎるか、単に明確ではありません。私の質問は次のとおりです。Pythonのselect()ステートメントをウィンドウで使用できる方法はありますか? 少し例を追加するか、上記のコードを変更してください。ありがとう!

4

3 に答える 3

9

sys.stdinが好きではないように見えます

入力をこれに変更すると

input = [server] 

例外はなくなります。

これはドキュメントからのものです

 Note:
    File objects on Windows are not acceptable, but sockets are. On Windows, the
 underlying select() function is provided by the WinSock library, and does not 
handle file descriptors that don’t originate from WinSock.
于 2012-05-31T23:54:40.720 に答える
5

あなたのコードに他の問題があるかどうかはわかりませんが、あなたが得ているエラーは に渡すためです。input問題は、ソケットではないselect.select()ものが含まれていることです。sys.stdinWindows では、selectソケットのみで動作します。

補足として、inputは Python 関数であるため、変数として使用することはお勧めできません。

于 2012-05-31T23:57:54.733 に答える