3

基本的に、私の考えは、コンピューターに接続してリモートでコマンドを実行できる、ある種の基本的なサーバーを作成することでした。これはそれほど問題にはならなかったようです。しかし、次のステップは論理的に、複数の接続を生成できるように、ある種のスレッドを追加することであるという明るい考えがありました。

multiprocessing.Process私は、GILのおかげで、これを試みるのが最善だと読んだ。私はスレッドを完全には理解しておらず、スレッドに関する適切なドキュメントを見つけるのは困難です。だから私はただ物を投げて、それがどのように機能するかを理解しようとしているようなものです。

まあ、私はこれを正しく行うことに近いかもしれないようです。しかし、私はこれを正しく行うにはほど遠い可能性が高いと感じています。私のプログラムは現在、複数の接続を許可していますが、最初にスレッドを使い始めたときは許可されていませんでした。ただし、接続が確立されてから別の接続が確立されると、最初の接続はサーバーにコマンドを送信できなくなります。誰かが私に助けを与えてくれるか、私が学び、理解する必要があることについて正しい方向に私を向けてくれるなら、私はそれをいただければ幸いです。

これが私のコードです:

class server:
    def __init__(self):
        self.s = socket.socket()
        try:
            self.s.bind(("",69696))
            self.s.listen(1)
        except socket.error,(value,message):
            if self.s:
                self.s.close()
    def connection(self):
        while True:
            client , address = self.s.accept()

            data = client.recv(5)
            password = 'hello'
            while 1:
                if data == password:
                    subprocess.call('firefox')
                    client.close()
                else:
                    client.send('wrong password')
                    data = client.recv(5)
            p = Process(target=x.connection())
            p.start()
x = server()

if __name__ == '__main':
    main()
4

1 に答える 1

1

この回答は、UNIX または UNIX に似たオペレーティング システムを使用している場合にのみ適用されます (Windows には、私たちが使用するものはありませんos.fork())。

UNIX プラットフォームでこれらのことを行うための最も一般的なアプローチの 1 つは、マスター プロセスがリクエストをリッスンし続けている間に、クライアント接続を処理する新しいプロセスをforkすることです。

以下は、複数の同時接続を処理できる単純なエコー サーバーのコードです。handle_client_connection()ニーズに合わせて変更するだけです

import socket
import os

class ForkingServer:
    def serve_forever(self):
        self.s = socket.socket()
        try:
            self.s.bind(("", 9000))
            self.s.listen(1)
        except socket.error, (value,message):
            print "error:", message
            if self.s:
                self.s.close()
            return

        while True:
            client,address = self.s.accept()
            pid = os.fork()
            # You should read the documentation for how fork() works if you don't
            # know it already
            # The short version is that at this point in the code, there are 2 processes
            # completely identical to each other which are simulatenously executing
            # The only difference is that the parent process gets the pid of the child
            # returned from fork() and the child process gets a value of 0 returned
            if pid == 0:
                # only the newly spawned process will execute this
                self.handle_client_connection(client, address)
                break
            # In the meantime the parent process will continue on to here
            # thus it will go back to the beginning of the loop and accept a new connection

    def handle_client_connection(self, client,address):
        #simple echo server
        print "Got a connection from:", address
        while True:
            data = client.recv(5)
            if not data:
                # client closed the connection
                break
            client.send(data)
        print "Connection from", address, "closed"


server = ForkingServer()
server.serve_forever()
于 2013-03-04T23:44:04.710 に答える