2017-07-29 12 views
0

を送信しませんこんにちは、私は、単純な管理アプリケーションウィッヒを記述しようとしています私のコンピュータシェルtroughtのtelnetのへのアクセスを提供します(これはPythonプログラミングの練習のための唯一のテストです)私がに接続するとき私のサーバーは、それから私は、端末(のWindows telnetクライアント)にのみ、黒い画面を持っていますが、私のプログラムのログにありoutpuがサブプロセスを形成し、それは私が多くのソリューションのためにGoogleで検索しましたが、それらのどれも働いていないクライアント にsenddedますないsdoesツイストLIBで適切と結果は同じ単純な管理ツイストハングをもとにPythonでアプリケーションおよびデータ

私のサーバーのコードだった:

# -*- coding: utf-8 -*- 

from subprocess import Popen, PIPE 
from threading import Thread 
from Queue import Queue # Python 2 

from twisted.internet import reactor 
from twisted.internet.protocol import Factory 
from twisted.protocols.basic import LineReceiver 
import sys 

log = 'log.tmp' 

def reader(pipe, queue): 
    try: 
     with pipe: 
      for line in iter(pipe.readline, b''): 
       queue.put((pipe, line)) 
    finally: 
     queue.put(None) 

class Server(LineReceiver): 

    def connectionMade(self): 
     self.sendLine("Creating shell...") 
     self.shell = Popen("cmd.exe", stdout=PIPE, stderr=PIPE, bufsize=1, shell=True) 
     q = Queue() 
     Thread(target=reader, args=[self.shell.stdout, q]).start() 
     Thread(target=reader, args=[self.shell.stderr, q]).start() 
     for _ in xrange(2): 
      for pipe, line in iter(q.get, b''): 
       if pipe == self.shell.stdout: 
        sys.stdout.write(line) 
       else: 
        sys.stderr.write(line) 
     self.sendLine("Shell created!") 

    def lineReceived(self, line): 
     print line 
     #stdout_data = self.shell.communicate(line)[0] 
     self.sendLine(line) 


if __name__ == "__main__":  
    ServerFactory = Factory.forProtocol(Server) 

    reactor.listenTCP(8123, ServerFactory) #@UndefinedVariable 
    reactor.run() #@UndefinedVariable 

答えて

0

あなたは、非ブロッキングプログラムをブロックするプログラムを混合します。ブロッキングパーツがブロックされているため、ノンブロッキングパーツは動作しません。彼らが実行して、非ブロック部品に依存しているため、ブロッキング部分は動作しません。

PopenQueueThreadを取り除き、代わりにreactor.spawnProcessを使用してください。または、Twistedを取り除き、ネットワーキングにもっと多くのスレッドを使用してください。

関連する問題