分类: Python/Ruby
2010-12-29 01:31:24
先用 web.config 配置 smtp
web.config.smtp_server = 'smtp.gmail.com'
web.config.smtp_port = 587
web.config.smtp_username = 'cookbook@gmail.com'
web.config.smtp_password = 'secret'
web.config.smtp_starttls = True
再用类似下边的发邮件
web.sendmail('cookbook@gmail.com', 'user@example.com', 'subject', 'message')
或者可以附上邮件头
web.sendmail('cookbook@webpy.org', ['user@example.com', 'user2@example.com'],
'subject', 'message',
cc='user1@example.com', bcc='user2@example.com',
headers=({'User-Agent': 'webpy.sendmail', 'X-Mailer': 'webpy.sendmail',})
)
class example:
def GET(self):
referer = web.ctx.env.get('HTTP_REFERER', '')
useragent = web.ctx.env.get('HTTP_USER_AGENT')
raise web.seeother(referer)
web.py 支持模板(注意需要 python-cheetah)
先看看将 第一个程序 改为使用模板
写入 templates/hello.html :
$def with (name, todos={})
$if name:你好,$name!
$else:你好,世界!
注意模板文件首行要个 $def with() ,
在 code.py 里用
render = web.template.render('templates/')
class hello:
def GET(self, name):
return render.hello(name)
Look, a $string.
Hark, an ${arbitrary + expression}.
Gawk, a $dictionary[key].function('argument').
Cool, a $(limit)ing.
Stop, \$money isn't uated.
We use basically the same semantics as (rejected) . Variables can go anywhere in a document.
If you put a backslash \
at the end of a line \
(like these) \
then there will be no newline.
这会输出一整行
Here are some expressions:
$for var in iterator: I like $var!
$if times > max:
Stop! In the name of love.
$else:
Keep on, you can do it.
$try:
$get(input)
$except:
Couldn't find it.
That's all, folks.
$# Here's where we hoodwink the folks at home:
Please enter in your deets:
CC: [ ] $#this is the important one
SSN: $#Social Security Number#$ [ ]
$# 到行末的是注释
可以将 python 语句放在行首的 "$ " 后(从"$ " 开始,之道行尾,或下一个 "$" )
$def with()
$ mapping = dict(a=[1, 2, 3],
$ b=[4, 5, 6])
$mapping['a'][0]
可以这样设置模板里的全局变量
len = 'cn'
web.template.Template.globals[len] = len
或者直接传递 globals()
web.template.Template.globals = globals()
也可以在 metaclass.func 里传递 locals()
class index:
def GET(self):
title = '你好,世界'
entrys = ['第一个', '第二个', '第三个']
s = web.Storage(locals())
return render.index(s)
而 templates/index.html 里用
$def with(s)
...$s.title
...
复选框有重复的表单项,譬如 id=10&id=20 这样的请求,可以用类似下边代码获得多个id 项值
class SomePage:
def GET(self):
user_data = web.input(id=[])
return "" + ",".join(user_data.id) + "
"
import web
urls = ('/upload', 'Upload')
class Upload:
def GET(self):
return """
web.py 0.3 有更好的 db 处理
import web
db = web.database(dbn='postgres', db='todo', user='you', pw='')
db.select('todo')
db.select('todo', where='id=$id', vars={'id': 2})
db.query('SELECT * FROM todo')
chinaunix网友2011-01-02 12:04:03
很好的, 收藏了 推荐一个博客,提供很多免费软件编程电子书下载: http://free-ebooks.appspot.com