python實現用程式給自己發郵件

2021-07-12 03:03:44 字數 2577 閱讀 9721

之前跑的實驗總是很耗時,有時候讓程式跑著自己出去玩,又得經常回來看它有沒有跑完。

於是乾脆寫個監測程式,讓它每隔一段時間就給我發個郵件。

當然,你要設定乙個監測條件。

比如我的主程式執行後會生成一些新檔案,那麼監測程式就每隔一段時間向我的郵箱報告一下現在生成了多少檔案。

這樣我在手機上就能知道主程式執行的怎麼樣了。

**在:

使用注意事項:

1. 使用前,首先要有兩個郵箱,乙個用於傳送資訊,乙個接收。當然這兩個郵箱可以是同乙個。

2. 然後修改程式裡面的郵箱和郵箱密碼。

3. 設定自己的傳送間隔和傳送資訊等。

下面是乙個簡化的版本。

發資訊的python程式:

# -*- coding:utf-8 -*-

# author: cslzy

# email: [email protected]

# description:send something to someone.

# date: 20151104 18:33:08

import smtplib

from email.mime.text import mimetext as mt

# some information should be changed to you own

mail_host = "you email server such as: smtp.163.com" # set you mail host

host_user = "chang to you email address as a host, such as: [email protected]" # host to send email

host_password = "***xxthis is not useable******chang to you own" # password of host

email_subject = "test message" # the subject of you email

reciever_address = "such as: [email protected]" #

send_message = "this is a test. oops..."

def sendto(address, message):

msg = mt(message, _subtype='plain')

msg['subject'] = email_subject

msg['form'] = host_user

msg['to'] = address

try:

server = smtplib.smtp()

server.connect(mail_host)

server.login(host_user, host_password)

server.sendmail(host_user, address, msg.as_string())

server.close()

return true

except exception, e:

print str(e)

return false

if __name__ == '__main__':

sendto(reciever_address,send_message)

監測檔案數量的程式:

# -*- coding:utf-8 -*-

# author: cslzy

# email: [email protected]

# description: if the files number is enough, send email to me.

# date: 20151104 18:52:45

from send_email import sendto

import os

import time

flag = true

reciever_email = '[email protected]' # you should change to you own

sleepy_time = 3 * 3600

print 'send message to %s every %d seconds'%(reciever_email, sleepy_time)

i = 0

while flag:

i += 1

print 'the %d\'th time to check'%i

message = ''

dirpath = '.'

if os.path.exists(dirpath):

listdir = os.listdir(dirpath)

message += 'there are %d file in %s\n'%(len(listdir), dirpath)

sendto(reciever_email, message)

# sleep

time.sleep(sleepy_time)

python123登入 python實現使用者登入

1.實現使用者輸入使用者名稱和密碼,當使用者名為 seven 且 密碼為 123 時,顯示登陸成功,否則登陸失敗 tag true while tag name input name passwd input password if name seven and passwd 123 print l...

python實現用socket傳輸檔案

python傳輸檔案最重要的有兩步 1 將要傳輸的檔案的資訊傳送過去,包括檔案包,大小以及其它資訊 2 傳送端讀取檔案內容並傳送過去,接受端將快取裡面的內容寫入檔案.傳送端 coding cp936 from socket import import os import struct addr 19...

python實現使用者答題功能

python實戰,使用者答題分享給大家。主要包含內容,檔案的讀取,更改,儲存。不同資料夾引入模組。輸入,輸出操作。隨機獲取資料操作 隨機生成算數表示式,使用者輸入答案,正確記錄分數,錯誤返回0,並把使用者分數記錄到文字檔案中,如使用者名稱不存在著新建使用者 mypythonfunction.py包含...