1

I'm trying to do some python, the idea is that when a special key on the keyboard is pressed in this case $ and * it will make a web request to my server.

It works but only once, so if I type for example $ it will send the request, but if I type this again or * it doesn't work. So I think it's because it's breaking the loop because of the keyboard.is_pressed() and I don't know how to fix that

Here's the code:

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()
martineau
  • 119,623
  • 25
  • 170
  • 301
Drakeee0909
  • 67
  • 1
  • 8
  • 1
    You could easily tell if the loop is being broken or not by using a debugger to step through your code. You really really should know how to do that from day 1 as a programmer of any kind. – Random Davis Oct 29 '21 at 17:38
  • Have any ideas to solve my issue ? – Drakeee0909 Oct 29 '21 at 17:42
  • Your code works perfectly for me. How do you know it's not working? Do you actually have other code following this? – Tim Roberts Oct 29 '21 at 17:54
  • And, by the way, this is a tight 100% CPU loop. You really need to add a `time.sleep(0.5)` in there to avoid chewing up an entire CPU. – Tim Roberts Oct 29 '21 at 17:55
  • Or use good old `input()`. Is there a reason not too? Is this part of a larger project - like a game? Make sure you have created a [mcve]. – 001 Oct 29 '21 at 17:56
  • In this case, I can't use input() – Drakeee0909 Oct 29 '21 at 18:03

2 Answers2

0

How about adding 'continue' at the end of your if and elif?

like this:

import http.client
import keyboard
import time

keep_looping = True
while keep_looping:
    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()
        time.sleep(0.5)

    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()
        time.sleep(0.5)

    elif keyboard.is_pressed('/'):  # Add this in so you can stop the loop when needed. Use any keypress you like.
        keep_looping = False
Harry Lee
  • 150
  • 9
0

In this case, there were two issues:

first, my server wasn't giving any responses, but the code wanted to get a response, so it was waiting a response. So I removed this code on the if and elif part

 res = conn.getresponse()
    data = res.read()

and secondly, I tried to create two http.client.HTTPConnection() with the same name, so it was giving me an error, so I changed the second to conn2 and same for headers2 and payload2

import http.client
import keyboard
import time

while True:
    if keyboard.is_pressed('Page_Up'):
        conn = http.client.HTTPConnection('server_ip:server_port')
        headers = {'Content-Type': "application/json",'Accept': "application/json"}
        payload = "{\n\t\"value\" : 0\n}"
        conn.request("POST", "/api", payload, headers)
        time.sleep(0.5)

    elif keyboard.is_pressed('Page_Down'):
        conn2 = http.client.HTTPConnection('server_ip:server_port')
        headers2 = {'Content-Type': "application/json",'Accept': "application/json"}
        payload2 = "{\n\t\"value\" : 1\n}"
        conn2.request("POST", "/api", payload2, headers2)
        time.sleep(0.5)
Drakeee0909
  • 67
  • 1
  • 8
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 29 '21 at 19:40
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 30 '21 at 02:53