通过HTTPServer执行shell命令-该命令仅在请求期间有效

问题描述 投票:0回答:1

为什么subprocess.call("xdotool key XF86AudioPlay")中的do_GET只在我的http请求期间持续?

我正在尝试通过http请求在本地计算机上播放/暂停Spotify。即。当收到请求时,模拟按键。

[当我按下localhost:8002时-音乐播放了约200ms,但请求完成后,它就会停止。

import http.server
import socketserver
import subprocess

MYCMD = "xdotool key XF86AudioPlay"
PORT = 8002

class MyRequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        # Send the html message
        subprocess.call(MYCMD, shell=True)
        self.wfile.write(b'works')

        return

Handler = MyRequestHandler

with socketserver.TCPServer(("", PORT), Handler) as httpd:
    print("serving at port", PORT)
    httpd.serve_forever()
python
1个回答
0
投票
通常subprocess.call()等待调用过程完成。

在您的示例中,它只是模拟了在服务器xwindows上按下按键的过程。您总结按下一个键即可切换播放音频。

我期望的是,您将以某种方式执行两个http请求。第一个开始音频,第二个停止音频。

可能两个请求以大约20ms的延迟执行。

我建议在子流程调用之前添加以下几行

with open("/tmp/mylogfile.log", "a") as fout: fout.write("call subprocess")

以及子流程调用后的两行。

使用open(“ / tmp / mylogfile.log”,“ a”)作为fout:fout.write(“称为子进程”)

我很确定,您会看到两个而不是一个请求。

© www.soinside.com 2019 - 2024. All rights reserved.