Python

fCheckJustOneProcess

def fCheckJustOneProcess(self):

call = 'ps -w | grep astrosafesys.py'
ps = subprocess.Popen("ps -w | grep python", shell=True, stdout=subprocess.PIPE)
output = ps.stdout.read()
ps.stdout.close()
ps.wait()
list = output.split();
for word in list: 
if word=="astrosafesys.py" and list.count(word)>1:
return False
return True


Read and write files

filename="resets.txt"
import os.path =0 
if os.path.isfile(filename):  
# Legge un file.  
in_file = open(filename,"r")  num = in_file.read()  in_file.close()  if num=="": num=0  num=int(num) + 1 print num # Scrive un file. out_file = open(filename,"w") out_file.write(str(num)) out_file.close()  print "END"

Check time

start = datetime.datetime.now() 
cmd=shlex.split("ping -c1 " + addr)
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while proc.poll() is None:   
    now = datetime.datetime.now()
    if (now - start).seconds> timeout:

HTTP POST

try:

            httpServ = httplib.HTTPConnection("yourwebsite.com", port, timeout=20)
            httpServ.connect()
            headers = {"Content-type": "application/x-www-form-urlencoded"}
             
            httpServ.request('POST', '/getFile.php', 'data='+image_64, headers)
             
            response = httpServ.getresponse()        
            if response.status == httplib.OK:                
                print "Output from CGI request:"
                lines = response.read().split('\n')
                for line in lines:
                    print line.strip()
                
            else:                
                print "Output from CGI request:"
                lines = response.read().split('\n')
                for line in lines:
                    print line.strip()
                
            httpServ.close()
        except Exception as e:
            print("HTTP Exception " + str(e))  

Send Email

#!/usr/bin/python
import time
import sys 
import smtplib                                                
                                                     
TO = 'email destination address'
GMAIL_USER = 'gmailuser'
GMAIL_PASS = 'gmailpw'
SUBJECT = 'email subject'
TEXT = 'email body'
TEXT2 =  'email body2'
                        
                                                     
#function to send email
def send_email():
  print("Sending Email")
  smtpserver = smtplib.SMTP("smtp.gmail.com",587)
  smtpserver.ehlo()
  smtpserver.starttls()
  smtpserver.ehlo
  smtpserver.login(GMAIL_USER, GMAIL_PASS)
  header = 'To:' + TO + '\n' + 'From: ' + GMAIL_USER
  header = header + '\n' + 'Subject:' + SUBJECT + '\n'
  print header
  msg = header + '\n' + TEXT + ' \n\n' + TEXT2 + ' \n\n'
  smtpserver.sendmail(GMAIL_USER, TO, msg)
  smtpserver.close()

send_email()