Python Forum
Beginner help: loops, if, else
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beginner help: loops, if, else
#1
Hello everyone,

Now I am nearly done with this mini 'project' but being so inexperienced with python I am trying make the code run until the user terminates the script. I have tried crontab but it doesnt appear to be working for some reason which I will investigate later.

Instead I have tried to initiate a loop to ensure that the script checks the email inbox repeatedly but I keep making mistakes and looping the incorrect part of the code.

import poplib
import webbrowser
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('USERNAME')
pop_conn.pass_('PASSWORD!')


#Get messages from server:
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#Parse message intom an email object:
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    if "bananas are the greatest nut" in message['subject'].lower():
        webbrowser.open('/home/pi/Desktop/PIC.jpg')


pop_conn.quit()


Would I put the loop in the section where the code gets the messages from the server? I have tried a simple x=1 while x=1 and while true but ive ended up in infinite loops breaking the machine im working on. I just want to loop the bit to get the messages and then if the subject "bananas are the greatest nut" is found then it executes the webbrowser function. Can anyone advise?
Reply
#2
Here's something that will capture ctrl-c
# This module will run forever until ctrl-c is pressed, at which point, 
# control is passed to function sig_capture
from time import sleep
import signal
import sys

def Im_sleeping():
    while True:
        print('zzz ... zzz ... zzz')
        sleep(2)

def sig_capture(signal, frame):
    print('someone inturepted me while I was sleeping!')
    sys.exit(0)

if __name__ == '__main__':
    signal.signal(signal.SIGINT, sig_capture)
    Im_sleeping()
Reply
#3
Thanks Larz60+, I have tried this but again it is opening multiple windows and never stopping!

import poplib
import webbrowser
from email import parser
from time import sleep
import signal
import sys

def Im_sleeping():
    while True:
        pop_conn = poplib.POP3_SSL('pop.gmail.com')
        pop_conn.user('USER')
        pop_conn.pass_('PASSWORD')

        messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
        messages = ["\n".join(mssg[1]) for mssg in messages]
        messages = [parser.Parser().parsestr(mssg) for mssg in messages]


        for message in messages:
            if "bananas are the greatest nut" in message['subject'].lower():
                webbrowser.open('/home/pi/Desktop/PIC.jpg')

def sig_capture(signal, frame):
    print('someone inturepted me while I was sleeping!')
    sys.exit(0)

if __name__ == '__main__':
    signal.signal(signal.SIGINT, sig_capture)
    Im_sleeping()
    


pop_conn.quit()
Am I using the code incorrectly?

I just thought, I didnt give much background.

So this is very simple I just want the script to query the mailbox and if there is a message with the subject 'bananas are the greatest nut' I would like it to open the picture specified. This works perfectly when used in isolation but as soon as I introduce loops I end up with an endless list of pictures constantly opening and never stopping. I just need it to open once and then only again once there is another email with the same subject.
Reply
#4
you don't need line 33
I can't understand why it doesn't quit. are you typing ctrl-c?

if you try the script I posted verbatim, you will see that it works.
the only other explanation that I can think of is that one of the modules you're using is servicing interrupts before the sig_capture.
also, if you are not running this from command line, then line 28 will never get executed and must be moved.
Reply
#5
(Jul-22-2018, 08:53 PM)Larz60+ Wrote: you don't need line 33
I can't understand why it doesn't quit. are you typing ctrl-c?

if you try the script I posted verbatim, you will see that it works.
the only other explanation that I can think of is that one of the modules you're using is servicing interrupts before the sig_capture.
also, if you are not running this from command line, then line 28 will never get executed and must be moved.

Yeah it works fine now thank you. my only problem is stopping the code from constantly opening windows once it sees the message in the inbox. I thought I could get around this by time.sleep(60) but it just messes up the code. Perhaps I could get it to delete the message after opening the window?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python beginner - nested while loops mikebarden 1 1,838 Jun-01-2020, 01:04 PM
Last Post: DPaul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020