我正在尝试做一些 python,其想法是当在这种情况下按下键盘上的特殊键 $ 和 * 时,它将向我的服务器发出 Web 请求。
它只工作一次,所以如果我输入例如 $ 它将发送请求,但如果我再次输入它或 * 它不起作用。所以我认为这是因为它打破了循环,因为 keyboard.is_pressed() 我不知道如何解决这个问题
这是代码:
import http.client
import keyboard
while True:
if keyboard.is_pressed('*'):
conn = http.client.HTTPConnection('server_ip:server_port')
payload = "{\n\t\"value\" : 0\n}"
headers = {'Content-Type': "application/json",'Accept': "application/json"}
conn.request("POST", "/api", payload, headers)
res = conn.getresponse()
data = res.read()
elif keyboard.is_pressed('$'):
conn = http.client.HTTPConnection('server_ip:server_port')
payload = "{\n\t\"value\" : 1\n}"
headers = {'Content-Type': "application/json",'Accept': "application/json"}
conn.request("POST", "/api", payload, headers)
res = conn.getresponse()
data = res.read()