2009-10-19
Send mail from gmail
python | |
I tested to send mail from gmail by python. It will send to mail what Livedoor Weather Web Service information. I tried to get weather informations of kyoto city. If you want to change city,please change city number and day parameter.
# -*- coding: utf-8 -*- import urllib2 import sys import xml.dom.minidom from email.MIMEText import MIMEText from email.Header import Header from email.Utils import formatdate import smtplib class Load(object): def __init__(self,spot=None): self.url = "http://weather.livedoor.com/forecast/webservice/rest/v1?city=%s&day=%s" if spot != None: (self.spot,self.day) = spot.values() self.url = self.url % (self.spot,self.day) else: sys.exit() print "Spot is:",self.url def pasu(self,nodelist): ret = [] node_obj = nodelist.childNodes for i in node_obj: return i.data def u_read(self): area_list= [] opener = urllib2.urlopen(self.url).read() tree = xml.dom.minidom.parseString(opener) title = tree.getElementsByTagName("title")[:3] celsius = tree.getElementsByTagName("celsius") for i in title: area_list.append(self.pasu(i)) for j in celsius: area_list.append(self.pasu(j)) weather_data = "\n".join(area_list).encode("ISO-2022-JP") return weather_data class Send_mail(object): def __init__(self,subj,from_add,to_add,password): self.subj = subj self.from_add = from_add self.to_add = to_add self.password = password def build(self,mess_body): msg_obj = MIMEText(mess_body,"plain","ISO-2022-JP") msg_obj["Subject"] = Header(self.subj,"ISO-2022-JP") msg_obj["From"] = self.from_add msg_obj["To"] = self.to_add msg_obj["Date"] = formatdate() return msg_obj def login(self,opa): send_obj = smtplib.SMTP("smtp.gmail.com", 587) send_obj.starttls() send_obj.login(self.from_add,self.password) send_obj.sendmail(self.from_add,self.to_add,opa.as_string()) send_obj.quit() if __name__ == '__main__': x = Load({"city":"79","day":"tomorrow"}) send_msg = x.u_read() title = u"天気のメール送ります" from_add = "your mail address" to_add = "send to address" password = "gmail password" mail = Send_mail(title,from_add,to_add,password) send = mail.build(send_msg) mail.login(send)
It referred.
Pythonでメールを送信する
http://labs.unoh.net/2007/06/python_2.html
お天気Webサービス仕様
http://weather.livedoor.com/weather_hacks/webservice.html
Analyzes urchin logs
perl | |
I wrote perl for the first time. This is analyze logs of urchin if you input IP address what would know user action
Result is url, it was seen pages and all of pages which user has seen it. Perl is useful when I have to spot job. Please you don't worry about strange variable names
open(IN,"log.txt"); @file = <IN>; $len = $#file; $pages; $ip = "input IP address area"; @mora; sub search_q { my ($moji) =@_; my $findit = index($moji,"http://www\.go-baaan\.com/"); if($findit >= 0){ $findit++; }else{ $findit = 0; } return $findit; } sub read_data { my ($all_len,$startline) =@_; if($startline != 0){ my $slice = substr($all_len,$startline-2); return $slice; } } sub accessed { my (@biji) =@_; my %co; my @aroro = grep{!$co{$_}++} @biji; return @aroro; } while($len > 0) { @list = split(/ /,@file[$len]); if(@list[0] eq $ip){ $s_num = &search_q(@file[$len]); $kita = &read_data(@file[$len],$s_num); @wake = split(/\"/,$kita); push(@mora,@wake[1]); $pages+=1; } $len--; } @urls = &accessed(@mora); $saw = $#urls; for($i = 0;$i<=$saw;$i++){ print @urls[$i]."\n"; } print $ip." saw ".$saw." pages.All of ".$pages." pages";
コメントを書く