如何与活动流程/子流程通信?

2024-10-04 11:36:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个Python脚本,它将自己交给一个活动的进程终端。所以,如果我想发送“向世界问好!”或“禁止用户名坏用户”等

我尝试过使用子进程库,尽管我现在对它非常陌生;最初我使用的是套接字,并通过Rcon协议连接到服务。不过,如果Rcon协议暴露在互联网上,我发现这是一个重大的安全风险。你知道吗

我当前要测试的环境是一个Minecraft服务器。你知道吗

我知道Java应用程序产生的错误不是本地引起的,我认为这必须处理我在它上使用子进程或者没有正确执行子进程的问题。你知道吗

其他研究: Communicating with a running processhttps://docs.python.org/2/library/subprocess.html#subprocess.Popenhttps://docs.python.org/release/3.1.3/library/subprocess.html#subprocess.Popen.communicate

import subprocess
import threading

ENV = {}

proc = subprocess.Popen(
    ['java', '-jar', 'server.jar'], 
    stdin=subprocess.PIPE, 
    stdout=subprocess.PIPE, 
    env=ENV,
    encoding='utf8'
)


def console():
    while(True):
        cmd = input('> ')
        if cmd.strip() != '':
            out = proc.communicate(cmd)[0]
            print(out)
            #proc.stdin.write(cmd)
            #print(proc.stdout.readline())
        #proc.stdin.write('say Hello World!')
        #print(proc.stdout.readline())

t1 = threading.Thread(target=console)
t1.start()

期望值: 我希望看到我的命令通过进程的终端。你知道吗

电流输出:

python rcon.py
> say hello
[09:17:43] [main/WARN]: Ambiguity between arguments [teleport, destination] and [teleport, targets] with inputs: [Player, 0123, @e, dd12be42-52a9-4a91-a8a1-11c01849e498]
[09:17:43] [main/WARN]: Ambiguity between arguments [teleport, location] and [teleport, destination] with inputs: [0.1 -0.5 .9, 0 0 0]
[09:17:43] [main/WARN]: Ambiguity between arguments [teleport, location] and [teleport, targets] with inputs: [0.1 -0.5 .9, 0 0 0]
[09:17:43] [main/WARN]: Ambiguity between arguments [teleport, targets] and [teleport, destination] with inputs: [Player, 0123, dd12be42-52a9-4a91-a8a1-11c01849e498]
[09:17:43] [main/WARN]: Ambiguity between arguments [teleport, targets, location] and [teleport, targets, destination] with inputs: [0.1 -0.5 .9, 0 0 0]
[09:17:43] [main/INFO]: Loaded 0 recipes
[09:17:43] [main/INFO]: Loaded 0 advancements
[09:17:44] [Server thread/INFO]: Starting minecraft server version 1.13.2
[09:17:44] [Server thread/INFO]: Loading properties
[09:17:44] [Server thread/INFO]: Default game type: SURVIVAL
[09:17:44] [Server thread/INFO]: Generating keypair
[09:17:44] [Server thread/INFO]: Starting Minecraft server on *:25565
[09:17:44] [Server thread/INFO]: Using default channel type
[09:17:44] [Server thread/ERROR]: Encountered an unexpected exception
java.lang.IllegalStateException: failed to create a child event loop
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:88) ~[server.jar:?]
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:58) ~[server.jar:?]
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:47) ~[server.jar:?]
        at io.netty.channel.MultithreadEventLoopGroup.<init>(MultithreadEventLoopGroup.java:59) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:77) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:72) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.<init>(NioEventLoopGroup.java:59) ~[server.jar:?]
        at ub.f(SourceFile:55) ~[server.jar:?]
        at xn.a(SourceFile:16) ~[server.jar:?]
        at ub.a(SourceFile:111) ~[server.jar:?]
        at so.d(SourceFile:181) ~[server.jar:?]
        at net.minecraft.server.MinecraftServer.run(SourceFile:566) [server.jar:?]
        at java.lang.Thread.run(Unknown Source) [?:?]
Caused by: io.netty.channel.ChannelException: failed to open a new selector
        at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:177) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:151) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:127) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:36) ~[server.jar:?]
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84) ~[server.jar:?]
        ... 12 more
Caused by: java.io.IOException: Unable to establish loopback connection
        at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source) ~[?:?]
        at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source) ~[?:?]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:?]
        at sun.nio.ch.PipeImpl.<init>(Unknown Source) ~[?:?]
        at sun.nio.ch.SelectorProviderImpl.openPipe(Unknown Source) ~[?:?]
        at java.nio.channels.Pipe.open(Unknown Source) ~[?:?]
        at sun.nio.ch.WindowsSelectorImpl.<init>(Unknown Source) ~[?:?]
        at sun.nio.ch.WindowsSelectorProvider.openSelector(Unknown Source) ~[?:?]
        at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:175) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:151) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:127) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:36) ~[server.jar:?]
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84) ~[server.jar:?]
        ... 12 more
Caused by: java.net.SocketException: Unrecognized Windows Sockets error: 10106: socket
        at sun.nio.ch.Net.socket0(Native Method) ~[?:?]
        at sun.nio.ch.Net.serverSocket(Unknown Source) ~[?:?]
        at sun.nio.ch.ServerSocketChannelImpl.<init>(Unknown Source) ~[?:?]
        at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(Unknown Source) ~[?:?]
        at java.nio.channels.ServerSocketChannel.open(Unknown Source) ~[?:?]
        at sun.nio.ch.PipeImpl$Initializer$LoopbackConnector.run(Unknown Source) ~[?:?]
        at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source) ~[?:?]
        at sun.nio.ch.PipeImpl$Initializer.run(Unknown Source) ~[?:?]
        at java.security.AccessController.doPrivileged(Native Method) ~[?:?]
        at sun.nio.ch.PipeImpl.<init>(Unknown Source) ~[?:?]
        at sun.nio.ch.SelectorProviderImpl.openPipe(Unknown Source) ~[?:?]
        at java.nio.channels.Pipe.open(Unknown Source) ~[?:?]
        at sun.nio.ch.WindowsSelectorImpl.<init>(Unknown Source) ~[?:?]
        at sun.nio.ch.WindowsSelectorProvider.openSelector(Unknown Source) ~[?:?]
        at io.netty.channel.nio.NioEventLoop.openSelector(NioEventLoop.java:175) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoop.<init>(NioEventLoop.java:151) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:127) ~[server.jar:?]
        at io.netty.channel.nio.NioEventLoopGroup.newChild(NioEventLoopGroup.java:36) ~[server.jar:?]
        at io.netty.util.concurrent.MultithreadEventExecutorGroup.<init>(MultithreadEventExecutorGroup.java:84) ~[server.jar:?]
        ... 12 more
[09:17:54] [Server thread/ERROR]: This crash report has been saved to: D:\WorkSpace\Python\Apps\MC\crash-reports\crash-2019-01-09_09.17.54-server.txt
[09:17:54] [Server thread/INFO]: Stopping server
[09:17:54] [Server thread/INFO]: Saving worlds
[09:17:54] [Server Shutdown Thread/INFO]: Stopping server
[09:17:54] [Server Shutdown Thread/INFO]: Saving worlds

谢谢你,我将非常感谢任何关于这个问题的建议或指点。你知道吗


Tags: ioinfosourceserverinitchanneljavach