java - 即使在编码后写入标准输入时,Python 'A bytes like object is required'
问题描述
我的代码是:
import subprocess, os
#just write the command to the input stream
process = None
minecraft_dir = '.'
executable = 'java -jar server.jar'
while True:
command=input('cmd: ').encode()
if command==(b"start"):
os.chdir(minecraft_dir)
process = subprocess.Popen(executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
print("Server started.")
else:
print(command)
print(command, file=process.stdin)
它总是说需要一个类似字节的对象,即使我打印它并且它清楚地显示它是字节格式,即使我很少让它工作,它仍然什么都不做。
为什么会这样??
解决方案
该print
函数计算其输入的字符串表示形式,添加换行符,并将其输出到字符流。如果您有二进制流(例如stdin
您的进程),请使用write
:
print(command) # This will output "b'USER INPUT'" (with a leading b')
process.stdin.write(command + b'\n')
推荐阅读
- python - 如何在python中分组反转数组?
- c - 将 C 链接到 LLVM 中的对象
- cordova - 在 Google Play 商店中上传 apk 文件时出现 apksigner 错误 - MIN_SIG_SCHEME_FOR_TARGET_SDK_NOT_MET
- python - 如何在熊猫中对具有条件的列应用聚合函数
- docker - nginx docker 不重定向 gogs docker 容器
- python-3.x - 无法在 TTP(模板文本解析器)python 中访问以前的模板变量
- java - 如何使用 Mockito 框架模拟我的服务
- linux - tc(流量控制)是否适用于数据包或帧?
- c# - 启动第二个会话时,MVC 中的路由如何发生?
- python - Python PDM + 使用 pylint 预提交:找不到导入