python eg.TriggerEvent() Please check my code

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
mrdavisma
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

python eg.TriggerEvent() Please check my code

Post by mrdavisma » Sun Feb 15, 2015 1:25 pm

Hi
I am hoping someone can help with my code.
I am learning python (only just started) and I am writing a script to test my ip address and having it trigger EG Events based on the result. The trouble I am having is no matter where in the script I place
eg.TriggerEvent("connecting") it never executes until the end of the script. I have put plenty of prints in there to debug and I can see everything going in the correct order apart from eg.TriggerEvent("connecting")

Code: Select all

# This code will retrieve your public ip address and check to see if you are
#  connected to a vpn or not and then can recheck x amount of times with a 
#  delay of x seconds between checks to give a connection time to connect.
#  events can then be generated on the results

#Import required libraries.
import urllib2    # For ip retrieval.
import time       # Used for time delay.
import eg         # Required for triggering events.

time_delay = 1    # Replace number with required delay in seconds.
rept_count = 5    # Replace number with required amount of tries.




my_ip = urllib2.urlopen('http://ip.42.pl/raw').read()
uk="86."
us="208."
count = 0
delay = time_delay
repete = rept_count


if uk in str(my_ip):
    print("if 1") #Just for debug
    eg.TriggerEvent("Connecting")
    count = count + 1
    
while count < repete:
    print("while 1") #just for debug
    
   
    if uk in str(my_ip):
        print("Connecting")
        print(count) #Just for debug        
        count = count + 1
        time.sleep(delay)

    if count == repete:
        if uk in str(my_ip):
            print("Connection Failed?")
            print(count) #Just for debug
            eg.TriggerEvent("VPN Connection Failed")
    else:
        if us in str(my_ip):
            count = count + 6
            print("Connected")
            eg.TriggerEvent("VPN Connected")
Is it possible to get it to trigger before the while function? I could solve this using a macro but would like to learn why the script wont work.

Cheers
Mark

Dragon470
Experienced User
Posts: 205
Joined: Thu Feb 10, 2011 2:16 am

Re: python eg.TriggerEvent() Please check my code

Post by Dragon470 » Sun Feb 15, 2015 3:50 pm

This is not an issue of triggering before the while ends. It is a eventghost blocking issue while the script is running at all. This means that the script needs to run in a separate thread so the events can trigger during the rest of the script execution. I can probably get an example up by the end of the day if you need one.

Dragon470
Experienced User
Posts: 205
Joined: Thu Feb 10, 2011 2:16 am

Re: python eg.TriggerEvent() Please check my code

Post by Dragon470 » Sun Feb 15, 2015 6:10 pm

You can ignore some things as they were done because I am not behind vpn at all. (needed that so no infinite loop)

Code: Select all

# This code will retrieve your public ip address and check to see if you are
#  connected to a vpn or not and then can recheck x amount of times with a 
#  delay of x seconds between checks to give a connection time to connect.
#  events can then be generated on the results

#Import required libraries.
import urllib2    # For ip retrieval.
import time       # Used for time delay.
import eg         # Required for triggering events.
from threading import Thread #for the thread

time_delay = 5    # Replace number with required delay in seconds.
rept_count = 5    # Replace number with required amount of tries.




my_ip = urllib2.urlopen('http://ip.42.pl/raw').read()
uk="86."
us="208."
#count = 0  no longer used outside of thread
delay = time_delay
repete = rept_count

#threaded item (so once the rest of script runs this will still be running)
def ThisIsTheThread(my_ip,uk,us,delay,repete):
    count = 0
    if uk in str(my_ip):
        print("if 1") #Just for debug
        eg.TriggerEvent("Connecting")
        count = count + 1
        
    while count < repete:
        print "while " + str(count) #just for debug
        
       
        if uk in str(my_ip):
            print("Connecting")
            print(count) #Just for debug        
            count = count + 1
            time.sleep(delay)

        if count == repete:
            if uk in str(my_ip):
                print("Connection Failed?")
                print(count) #Just for debug
                eg.TriggerEvent("VPN Connection Failed")
        else:
            if us in str(my_ip):
                count = count + 6
                print("Connected")
                eg.TriggerEvent("VPN Connected")
            count = count + 1
            eg.TriggerEvent("NO VPN")
            
t = Thread(target=ThisIsTheThread, args=(my_ip,uk,us,delay,repete))
t.start() #after it starts the thread then eventghost is no longer blocking new trigger events.

mrdavisma
Posts: 11
Joined: Sat Jan 31, 2015 9:10 pm

Re: python eg.TriggerEvent() Please check my code

Post by mrdavisma » Mon Feb 16, 2015 12:23 pm

Thank you Dragon
I am only on the fist couple of chapters of python for noobs and I believe the chapter for threading is a long way off lol but after having a play around and a lot of debugging I have managed to get it working the way I want.
I did find out (after a while) that the orignal script would only check the my_ip once at the start then it would run the while loop based on that one result which obviously defeated the object! so I duplicated the command inside the while-loop and it works great.

Code: Select all

from threading import Thread #for the thread
import urllib2    # For ip retrieval.
import time       # Used for time delay.
import eg         # Required for triggering events.
time_delay = 3    # Replace number with required delay in seconds.
rept_count = 5    # Replace number with required amount of tries.




my_ip = urllib2.urlopen('http://ip.42.pl/raw').read()
uk="86."
us="208."
delay = time_delay
repete = rept_count

#threaded item (so once the rest of script runs this will still be running)
def ThisIsTheThread(uk,us,delay,repete,eg,time,urllib2):
    count = 0
       
    while count < repete:
        my_ip = urllib2.urlopen('http://ip.42.pl/raw').read()
        if uk in str(my_ip):
            print("Connecting")
            print(count) #Just for debug        
            count = count + 1
            time.sleep(delay)

        if count == repete:
            if uk in str(my_ip):
                print("Connection Failed?")
                print(count) #Just for debug
                eg.TriggerEvent("VPN Connection Failed")
        else:
            if us in str(my_ip):
                count = count + 10
                print("Connected")
                eg.TriggerEvent("VPN Connected")    
            
t = Thread(target=ThisIsTheThread, args=(uk,us,delay,repete,eg,time,urllib2))
t.start() #after it starts the thread then eventghost is no longer blocking new trigger events.
if uk in str(my_ip):
    eg.TriggerEvent("Connecting")
    
print(my_ip)   


Cheers for explaining why eventghost wouldn't fire the event and for pointing out threading to me

Regards Mark

Post Reply