How to transform this as a service

Maroso Marco androidmaroso at gmail.com
Tue Feb 20 17:43:01 EST 2018


Il giorno martedì 20 febbraio 2018 23:42:27 UTC+1, Maroso Marco ha scritto:
> Hi everyone, i need this program to run as a service.
> 
> This program basically check an email account (gmail) and looks if there is
> mail from a specific email account, then reads the subject line and splits the text found assigning the left part (before the ; ) to the password, and the right part to the comand to be executed.
> It should loop continuosly waiting a specific time given in a file called timing.ini (seconds)
> 
> My program seems to have a problem and i don't understand which! Firstly it worked and now just went to run it again and it doesnt. I have different versions of python installed on my pc.
> 
> Can someone please tell me how to transform it in a service to run on Windows?
> 
> I found some solutions but i don't understand how to implement it correctly
> This is my code, any help is really apreciated, i'm loosing my nights on this and can't get it working.
> 
> 
> 
> 
> import imaplib
> import os
> import email
> import email.header
> import time
> import subprocess
> 
> def mailcontroller():
>     
>     # Set user, pass and allowed mail for giving commands
>     plusmail = "anemailaddress at gmail.com"
>     googlepass = "thepassword"
>     captain = "autorizedemailaccount at gmail.com"
>     
>     # Set vars for IMAP access
>     M = imaplib.IMAP4_SSL('imap.gmail.com')
>     M.login(plusmail, googlepass)
>     M.select()
> 
>     # Set search on UNSEEN messages
>     status, response = M.search(None, '(UNSEEN)')
>     unread_msg_nums = response[0].split()
> 
> 
>     # Mark as read
>     for e_id in unread_msg_nums:
>         M.store(e_id, '+FLAGS', '\Seen')
> 
>     # cycle messages sent from autorized email address
>     typ, data = M.search(None, 'From',(captain))
> 
>     for num in data[0].split():
>         typ, data = M.fetch(num, '(RFC822)')
>         msg = email.message_from_string(data[0][1])
>         decode = email.header.decode_header(msg['Subject'])[0]
>         subject = unicode(decode[0])
>         comando = subject
> 
>         if googlepass in subject:
>             
>             # print 'Message %s: %s' % (num, subject)
>             # Split subject line
>             googlepass,comando = subject.split(";")
>             # Execute command
>             #os.system(comando)
>             # Execute command with alternate method
>             subprocess.call(comando)
>             # Delete email
>             M.store(num, '+FLAGS', '\\Deleted')
> 
>     M.close()
>     M.logout()
> 
>     # Read ini file for timer settings
>     timing = open('timing.ini', 'r').read()
>     # Convert timer value from string to int
>     time.sleep(int(timing))
>     
> while True:
>     mailcontroller()

I would like to implement my code in something like this described here : http://www.chrisumbel.com/article/windows_services_in_python



More information about the Python-list mailing list