TCPシングルスレッドの同時実行

サーバー

gevent 輸入サル; monkey.patch_all()
 輸入ソケット
 から gevent インポートスポーン


サーバー = socket.socket()
server.bind((' 127.0.0.1 '、8080 ))
server.listen( 5 


デフ話(CONN):
     しばらく真:
         試す
            データ = conn.recv(1024 の場合、LEN(データ)== 0:ブレーク
            プリント(data.decode(' UTF-8 ' ))
            conn.send(data.upper())
        除く:eとConnectionResetError
             印刷(e)の
             ブレーク
    はconn.close()

DEF SERVER1():
     しばらく真:
        CONN、ADDR = server.accept()
        産卵(講演、CONN)

であれば __name__ == ' __main__ ' 
    G1 = 産卵(サーバー1)
    g1.join()

クライアント

インポートソケット
 からスレッドインポートスレッド、current_thread 


DEF クライアント():
    クライアント = socket.socket()
    client.connect((' 127.0.0.1 '、8080 ))
    、N = 0
     一方TRUE:

        データ = ' %S%S '( current_thread()。名、N)
        client.send(data.encode(' UTF-8 ' ))
        RES = client.recv(1024 プリント(res.decode(' UTF-8' ))
        、N + = 1 のための I における範囲(400 ):
    T =スレッド(目標= クライアント)
    t.start()

 

おすすめ

転載: www.cnblogs.com/lddragon/p/11359251.html