谁能帮我解释下下面脚本的含义

sh/bash/dash/ksh/zsh等Shell脚本
回复
nameaj
帖子: 39
注册时间: 2009-01-01 14:06

谁能帮我解释下下面脚本的含义

#1

帖子 nameaj » 2012-05-20 10:04

#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import base64
import smtplib
import urllib.request
from subprocess import check_call, PIPE
iffile = '/tmp/ip'

def getlastip():
try:
return open(iffile).read()
except IOError:
return False
def getcurip():
return urllib.request.urlopen('http://icanhazip.com/').read().rstrip().decode()
def mailip(ip):
check_call(['/usr/local/sbin/cjb-update.sh'], stdout=PIPE)
s = smtplib.SMTP('smtp.126.com')
status = s.login('xxx', base64.decodebytes(b'yyy').decode())[0]
if status == 235:
msg = 'From: xxx\r\n'\
'To: yyy\r\n'\
'Subject: latest ip\r\n'\
'\r\n'
msg += ip
s.sendmail('xxx', 'yyy', msg)
else:
return False
s.quit()
def main():
while True:
curip = getcurip()
if not curip.strip():
# 有时会得到空内容
continue
if curip != getlastip():
open(iffile, 'w').write(curip)
mailip(curip)
break

if __name__ == '__main__':
main()
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: 谁能帮我解释下下面脚本的含义

#2

帖子 aerofox » 2012-05-20 10:19

贴代码贴在代码块中,尤其是python代码,不放代码块中,缩进都没了,代码本身就不正确了。
头像
枫叶饭团
帖子: 14683
注册时间: 2010-06-16 1:05
系统: Mac OS X
来自: Tencent
联系:

Re: 谁能帮我解释下下面脚本的含义

#3

帖子 枫叶饭团 » 2012-05-20 11:28

无力吐槽,这样子的py能看才奇怪了
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 谁能帮我解释下下面脚本的含义

#4

帖子 YeLee » 2012-05-20 12:58

[python]
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import base64
import smtplib
import urllib.request
from subprocess import check_call, PIPE
iffile = '/tmp/ip'
def getlastip():
try:
return open(iffile).read()
except IOError:
return False
def getcurip():
return urllib.request.urlopen('http://icanhazip.com/').read().rstrip().decode()
def mailip(ip):
check_call(['/usr/local/sbin/cjb-update.sh'], stdout=PIPE)
s = smtplib.SMTP('smtp.126.com')
status = s.login('xxx', base64.decodebytes(b'yyy').decode())[0]
if status == 235:
msg = 'From: xxx\r\n'\
'To: yyy\r\n'\
'Subject: latest ip\r\n'\
'\r\n'
msg += ip
s.sendmail('xxx', 'yyy', msg)
else:
return False
s.quit()
def main():
while True:
curip = getcurip()
if not curip.strip():
# 有时会得到空内容
continue
if curip != getlastip():
open(iffile, 'w').write(curip)
mailip(curip)
break
if __name__ == '__main__':
main()
[/python]
http://p.vim-cn.com/cGW/python
那么好看的代码居然被弄成这样,看的心情都没了。 :em04 :em04 :em04
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
头像
lilydjwg
论坛版主
帖子: 4258
注册时间: 2009-04-11 23:46
系统: Arch Linux
联系:

Re: 谁能帮我解释下下面脚本的含义

#5

帖子 lilydjwg » 2012-05-20 14:49

YeLee 写了:[python]
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import base64
import smtplib
import urllib.request
from subprocess import check_call, PIPE
iffile = '/tmp/ip'
def getlastip():
try:
return open(iffile).read()
except IOError:
return False
def getcurip():
return urllib.request.urlopen('http://icanhazip.com/').read().rstrip().decode()
def mailip(ip):
check_call(['/usr/local/sbin/cjb-update.sh'], stdout=PIPE)
s = smtplib.SMTP('smtp.126.com')
status = s.login('xxx', base64.decodebytes(b'yyy').decode())[0]
if status == 235:
msg = 'From: xxx\r\n'\
'To: yyy\r\n'\
'Subject: latest ip\r\n'\
'\r\n'
msg += ip
s.sendmail('xxx', 'yyy', msg)
else:
return False
s.quit()
def main():
while True:
curip = getcurip()
if not curip.strip():
# 有时会得到空内容
continue
if curip != getlastip():
open(iffile, 'w').write(curip)
mailip(curip)
break
if __name__ == '__main__':
main()
[/python]
http://p.vim-cn.com/cGW/python
那么好看的代码居然被弄成这样,看的心情都没了。 :em04 :em04 :em04
囧,我的空行呢?复制不过来?你什么浏览器?
头像
YeLee
论坛版主
帖子: 26406
注册时间: 2008-08-13 8:48
系统: Fundu i64
来自: 东海硇州,一双管钥。
联系:

Re: 谁能帮我解释下下面脚本的含义

#6

帖子 YeLee » 2012-05-20 16:18

lilydjwg 写了:
YeLee 写了:[python]
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import base64
import smtplib
import urllib.request
from subprocess import check_call, PIPE
iffile = '/tmp/ip'
def getlastip():
try:
return open(iffile).read()
except IOError:
return False
def getcurip():
return urllib.request.urlopen('http://icanhazip.com/').read().rstrip().decode()
def mailip(ip):
check_call(['/usr/local/sbin/cjb-update.sh'], stdout=PIPE)
s = smtplib.SMTP('smtp.126.com')
status = s.login('xxx', base64.decodebytes(b'yyy').decode())[0]
if status == 235:
msg = 'From: xxx\r\n'\
'To: yyy\r\n'\
'Subject: latest ip\r\n'\
'\r\n'
msg += ip
s.sendmail('xxx', 'yyy', msg)
else:
return False
s.quit()
def main():
while True:
curip = getcurip()
if not curip.strip():
# 有时会得到空内容
continue
if curip != getlastip():
open(iffile, 'w').write(curip)
mailip(curip)
break
if __name__ == '__main__':
main()
[/python]
http://p.vim-cn.com/cGW/python
那么好看的代码居然被弄成这样,看的心情都没了。 :em04 :em04 :em04
囧,我的空行呢?复制不过来?你什么浏览器?
fx 10.0.4 :em06 :em06 :em06
◎当我站在道德的高度上俯视别人的时候,发现自己是多么渺小。
♥执着但不偏激,反对而不排斥,坚决捍卫矛盾体的存在方式。
★★★天气预报★★★
fcitx-yatable一个可以使用的码表输入法
[教程]几个实例攻克软件编译难关
Gentoo Development Guide
字体相关
回复