python獲取本地IP位址傳送郵件

2021-08-15 20:40:42 字數 1408 閱讀 9263

#!/usr/bin/env python

#_*_coding:utf-8 _*_

import time

import socket

import fcntl

import struct

import smtplib

from email.mime.text import mimetext

def get_ip_add(ifname):

s=socket.socket(socket.af_inet,socket.sock_dgram)

return socket.inet_ntoa(fcntl.ioctl(

s.fileno(),

0x8915,

struct.pack('256s',ifname[:15])

)[20:24])

def sendmail(subject,msg,fromemail,emailpasswd,toemail):

user=fromemail

pwd=emailpasswd

to=toemail

nowtime=time.strftime('%y-%m-%d %h:%m:%s')

msg=mimetext(msg)

msg["subject"]=subject

msg["from"]=user

msg["to"]=to

try:

#s=smtplib.smtp_ssl('smtp.qq.com',465)

s=smtplib.smtp('smtp.126.com',25)#連線126服務端

s.login(user,pwd)

s.sendmail(user,to,msg.as_string())

s.quit()

print "[%s]info:email send success!"% nowtime

except smtplib.smtpexception,e:

print "[%s]error:email send faild,%s"%(nowtime,e)

if __name__=='__main__':

local_id=get_ip_add('em1')

print local_id

subject='伺服器[%s]日誌報警了!'%local_id#可以根據實際定義

fromemail='[email protected]'

emailpasswd='***xx'##這裡的密碼是客戶端的授權碼,不是126的登入密碼

toemail='[email protected]'#發給誰

name_1="this is tesing"

sendmail(subject,name_1,fromemail,emailpasswd,toemail)#name_1可以根據實際的需求寫對應的內容

獲取本地ip位址

之前寫了乙個自動收款的方法,使用的是springmvc定時器實現的,自動收款的方法,主要是針對現金業務的,實現對於業務員提交的收款單,自動收款的一系列操作來減輕 財務同事工作的,完成後在測試系統上測試沒問題,然後上線,上線後經常有幾筆會出現從重複收款的問題,我把 看了一遍又一遍,把日誌看了一遍又一遍...

Lua獲取本地IP位址

如何通過lua指令碼獲取本機的ip位址,需要用到socket.lua指令碼。local socket require socket print os.time function getadd hostname local ip,resolved socket.dns.toip hostname lo...

Python 獲取本地IP

使用撥號上網的話,一般都有乙個本地ip和乙個外網ip,使用python可以很容易的得到這兩個ip 使用gethostbyname和gethostbyname ex兩個函式可以實現 import socket localip socket.gethostbyname socket.gethostnam...