首页 文章

Python3:Popen通讯

提问于
浏览
0

我是游戏玩家和编码员 . 在我的一个游戏中,我需要一台服务器,所以,和朋友一起买了一台 . 但是,这位朋友并不了解编码的任何内容,尤其是Linux的bash,所以他无法打开/关闭我们的服务器,他也无法解决它 . 所以我决定制作一个帮助他使用我们服务器的程序 . 关键是服务器是用java编码的(它是一个jar),没有外部命令我无法访问它 . 所以,我花了很多文档,所有这些文档都对我说:“你必须使用子进程!”!!所以我用它,但是我在这个Popen对象中遇到了一个大问题注意问题:我的程序不只是打开/关闭服务器,它是一个服务器,其亲属客户端允许与Minecraft服务器交互,它使用一些套接字和线程libreries .

所以,问题是:

class getOutput(Thread):
def __init__(self,outProc,connection):
    self.proc = outProc                #My Popen object
    self.connection = connection       #a socket object (my server biding)
    self.stop = False                  #A way to stop this thread is to put getOutput.stop = True
    Thread.__init__(self)
def run(self):
    proc = self.proc
    conn = self.connection
    while True:
        if self.stop:                  #if getOutput.stop = True, stop the thread
            break
        out = proc.stdout.readlines()  #My problem...
        for lines in out:
            print(out)                 #My output where the server is started
            conn.send(line)            #send to client the output of the server

def StartMcServ():
    global proc, conn, outputManager, McServStarted          #Because it's a server and I must launch the minecraft server more than once
    print('[Server/Info] Started McServ!')                   #Info is always useful
    logfile.log('[Server/Info] Started McServ!')             #a librery that I created that log in a file, ignore this line
    proc = sh.Popen(JavaServ(),stdin=PIPE,stdout=PIPE,stderr=PIPE) #sh=subprocess, I did a from subprocess import PIPE,STDOUT ; import subprocess as sh because of the functions of subprocess have the same name that my libreries
    outputManager = getOutput(proc,conn)                     #My thread, that have the process and the connection
    outputManager.start()

def PutInput(*,Input=None):                                  #A function that put input in my server
    if Input and type(Input) == type(b'') and Input.endswith(b'\n'):
        proc.stdin.write(Input)

所以, I really don't know 有什么问题,但是与服务器的通信并没有't work, I' m sooooo对此感到难过

P.S . :由于它的长度,我没有把所有程序或主要程序放入:D

附: (2):如果我犯了英语错误,我很抱歉,但我13岁,我在学校学习

1 回答

  • 0

    解决方案是:

    def run(self):
        proc = self.proc
        conn = self.connection
        while True:
            if self.stop:
                break
            conn.send(proc.stdout.readline())
    

相关问题