网络通信就是两个进程之间的通信
使用SMTP发送邮件
:
# -*- coding: utf-8 -*-
#-*-coding:utf-8-*-
#==========================================
# 导入smtplib和MIMEText
#==========================================
from email.mime.text import MIMEText
import smtplib
#==========================================
# 要发给谁,这里发给3个人
#==========================================
mailto_list=["[email protected]"]
#==========================================
# 设置服务器,用户名、口令以及邮箱的后缀
#==========================================
mail_host="smtp.qq.com"
mail_user="1262966556"
mail_pass="crilcpgkflanfhda"#qq要求授权码,密码不行,你要在手机发短信具体的上qq邮箱网站上看
mail_postfix="qq.com"
#==========================================
# 发送邮件
#==========================================
def send_mail(to_list,sub,content):
'''''
to_list:发给谁
sub:主题
content:内容
send_mail("[email protected]","主题","内容")
'''
me=mail_user+"<"+mail_user+"@"+mail_postfix+">"
msg = MIMEText(content)
msg['Subject'] = sub
msg['From'] = me
msg['To'] = ";".join(to_list)
try:
s = smtplib.SMTP_SSL(mail_host, 465)
s.set_debuglevel(1)
s.login(mail_user,mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return True
except Exception, e:
print str(e)
return False
if __name__ == '__main__':
if send_mail(mailto_list,"lsw to 大比","大比"):
print "发送成功"
else:
print "发送失败"
JSON(JavaScript Object Notation) 轻量级的数据交换格式
CGI(Commen Gateway Iterface) 通用路由接口
Python联系实例27:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
# -*- coding: utf-8 -*- def output(s,l): if l==0: return print (s[l-1]) output(s,l-1) s=raw_input('Input a string') l=len(s) output(s,l)
Python联系实例28:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?
# -*- coding: utf-8 -*- def age(n): if n==1: c=10 else: c=age(n-1)+2 return c print age(5)
Python联系实例29:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字
# -*- coding: utf-8 -*- print 'input a number more than 10' n=input() x=str(n) print '一共',len(x),'位' for i in range(len(x)-1,-1,-1): print x[i],
# -*- coding: utf-8 -*- print 'input a number more than 10' n=input() x=str(n) for i in range(len(x)): print x[-i-1],
Python联系实例31:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母。
# -*- coding: utf-8 -*- weeklist = {'M': 'Monday','T': {'u': 'Tuesday','h':'Thursday'}, 'W': 'Wednesday', 'F':'Friday','S':{'a':'Saturday','u':'Sunday'}} sLetter1 = raw_input("请输入首字母:") sLetter1 = sLetter1.upper() if (sLetter1 in ['T','S']): sLetter2 = raw_input("请输入第二个字母:") print(weeklist[sLetter1][sLetter2]) else: print(weeklist[sLetter1])
按逗号分隔列表。
# -*- coding: utf-8 -*- L = [1,2,3,4,5] s1 = ','.join(str(n) for n in L) print s1
有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
a = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29]
b = input('输入要插入的数字')
a.append(b)
c = a[:]
l = len(c)
# 从后面开始,如果比倒数第二个数大,那就将新加入的数填在倒数第一的位置,否则倒数第二的数位置后移
for i in range(l,0,-1):
if (b>c[i-2]):
c[i-1] =b
break
else:
c[i-1] = c[i-2]
print c
模仿静态变量的用法。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
def varfunc():
var = 0
print 'var = %d' % var
var += 1
if __name__ == '__main__':
for i in range(3):
varfunc()
# 类的属性
# 作为类的一个属性吧
class Static:
StaticVar = 5
def varfunc(self):
self.StaticVar += 1
print self.StaticVar
print Static.StaticVar
a = Static()
for i in range(3):
a.varfunc()
#!/usr/bin/python
# -*- coding: UTF-8 -*-
X = [[12, 7, 3],
[4, 5, 6],
[7, 8, 9]]
Y = [[5, 8, 1],
[6, 7, 3],
[4, 5, 9]]
result = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
# 迭代输出行
for i in range(len(X)):
# 迭代输出列
for j in range(len(X[0])):
result[i][j] = X[i][j] + Y[i][j]
for r in result:
print(r)
画图,学用circle画圆形。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
if __name__ == '__main__':
from Tkinter import *
canvas = Canvas(width=800, height=600, bg='yellow')
canvas.pack(expand=YES, fill=BOTH)
k = 1
j = 1
for i in range(0, 26):
canvas.create_oval(310 - k, 250 - k, 310 + k, 250 + k, width=1)#(x0,y0,x1,y1,线的宽度)
k += j
j += 0.3
mainloop()