Random numbers

I have finally started to look in little depper into python rather than bash.
Looking at some no much sense project to write some script in python (2.7).

Below a small script which generate three bunches of number + 2 and sending it by email in html.

 

 

!/usr/bin/python

import random
import smtplib
import time
import os, sys
from pathlib import Path


### SOME VARIABLES TO SET

fromaddr = "senderemail"
toaddr = "recipientemail"

# TIME TO GET INTO THE FILE
timestr = time.strftime(" %d-%m-%Y-%Hh%M")


# CHECKS IF FILE IS ALREADY PRESENT

my_file = Path("numbers")
if my_file.is_file():
        os.remove("numbers")


f = open( 'numbers', 'a' )
print "- FILE CREATED"
f.write('' + '\n')
f.write('  ' + '\n')
f.write('  ' + '\n')
f.write('   


Hello,
' + '\n') f.write('

Find below your next Lotto:
' + '\n') f.write( '

Date:' +timestr + '

\n' ) f.write("\n") for i in range(1,4): strings = i list = [] i = 0 while i < 5: n = random.sample(xrange(1,50), 1) list.append (n) i += 1 str1 = ','.join(map(str, list)) s = str1.replace("[", "") s = s.replace("]", "") # GENERATE THE SECOND BUNCH list1 = [] j = 0 while j < 2: m = random.sample(xrange(1,10), 1) #m = random.randint(1, 10) list1.append (m) j += 1 str2 = ','.join(map(str, list1)) str2 = str2.replace("[", "") str2 = str2.replace("]", "") # WRITING ALL TO FILE f.write("

%s - %s - %s

\n" % (strings, s, str2 ) ) f.close() print "- NUMBERS GENERATED" f = open( 'numbers', 'a' ) f.write('


Your best team' + '\n') f.write('

' + '\n') f.write('' + '\n') f.write('' + '\n') f.close() from email.MIMEMultipart import MIMEMultipart from email.MIMEText import MIMEText msg = MIMEMultipart() msg['From'] = fromaddr msg['To'] = toaddr msg['Subject'] = "Your next lotto numbers" with open('numbers', 'r') as myfile: body=myfile.read().replace(',', ' ') # REMOVING PLAIN TEXT - KEEP ONLY HTML #msg.attach(MIMEText(body, 'plain', 'html')) #part1 = MIMEText(body, 'plain') part2 = MIMEText(body, 'html', 'html') #msg.attach(part1) msg.attach(part2) server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login(fromaddr, "mypassword") text = msg.as_string() server.sendmail(fromaddr, toaddr, text) print "- MESSAGE SENT" server.quit()

Here is the output that you would receive by email:

Hello,
Find below your next Lotto:
Date: 04-10-2016-18h44

1 - 44 16 19 49 48 - 2 1

2 - 35 14 49 31 14 - 7 9

3 - 5 10 9 5 11 - 6 3


Your best team

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

Menu