如何从头搭建一个搜索引擎_简易的网页框架web.py
来源:互联网 发布:七月算法 邹博 编辑:程序博客网 时间:2024/05/16 12:12
类别:电工导C
日期:2016年9月16日
标题:第四次实验_web.py
编号:4
一.web.py的使用:
- 框架(framework):某种应用的半成品,把不同的应用程序中具有共性的一部分拿出来,做成一个半成品程序,即为所谓框架
- 好处:减少重复的开发工作量,缩短开发时间,降低开发成本balabalabala,使程序更加稳定
- 几种框架平台:
- 重量级
- Zope
- 中量级
- Django
- Quixote
- 轻量级
- Pylons
- TurboGears
- 迷你级
- Tornado
- web.py
- Bottle & Flask
- 其他
- web2py、uliweb、Karrigell、Werkzeug …
- 网站的URL结构:
- 任何网站的最重要的部分就是URL结构,URL不仅是访问者访问网站的工具,而且也规定了网站的结构模型,在一些类似del.icio.us之类的网站,URL也是UI的一部分
- 如何把URL告诉web.py:
- 首先定义一个识别url的变量
urls = (
'/','index'
)
#index 为处理url的一个类,'/'为“域名/”这一个url,第一部分为一个正则表达式,用来匹配用户输入的变量
#NOTE:url中'?'后面的部分不会被正则表达式所匹配,而会被web.input(obj)方法作为用户GET方法的输入而截取,以字典的形式存储,obj为所要提取的变量名称,而使用GET会自动在/后加上一个?
- 创建一个列举这些url的application
app = web.application(urls,globals())
#globals()表示处理urls的类需要在全局变量的命名空间中寻找
- 如何定义处理urls的类?
#在全局变量命名空间
class index:
def GET(self):
return "hello world!"
#GET为HTTP协议的GET方法,类似的还有POST
#其余定义和一般class中的成员函数没有本质区别
- 如何让web.py运行起来?
if __name__ == "__main__":
app.run()
- 一个完整的程序:
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
return "Hello, world!"
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
- 启动服务
- 如果你在命令行下面,请输入:: $ python code.py http://0.0.0.0:8080/
- 现在你的web.py应用正运行在你电脑上的一个真正的web服务器上。 访问那个URL,然后你应该看到"Hello, world!" (你可以通过把IP地址/端口加在"code.py"的后面,来控制web.py在哪里启动服务器。你也可以让它运行在fastcgi或scgi服务器上)。
- 注意: 如果你不能或者不想使用默认端口,你可以使用这样的命令来指定端口号:
$ python code.py 1234
- 模板:
- 在 Python 中写 HTML 不是聪明的选择,相反在 HTML 中写 Python 则有趣的多。幸运的是,web.py 让这件事情做得简单而又漂亮。
- 注意: 老版本的 web.py 使用 Cheetah 模板系统,你可以也欢迎使用其他模板系统,但它可能不会被长久支持。
- 给模板新建一个目录(命名为 templates),在该目录下新建一个以 .html 结尾的文件,内容如下:
<em>Hello</em>, world!
- 你也可以在模板中使用 web.py 模板支持代码:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
- 简单的使用:
#在全局变量命名空间
render = web.template.render('templates/')
#在index class的get函数中写:(前提为在templates文件夹中已经有这个index模板)
return render.index(i.name)
- 模板的语法:
- 一条基本原则:在模板中使用python的表达式和变量时在前面加上$,例如
Look, a $string.
Hark, an ${arbitrary + expression}.
Gawk, a $dictionary[key].function('argument').
Cool, a $(limit)ing.
- 定义变量:有时想在模板中定义新变量,使用$空格或者$var
$ bug = get_bug(id)
<h1>$bug.title</h1>
<div>
$bug.description
<div>
#注意$后面的空格,是区别定义变量(有空格)和表达式(无空格)的方式
$def with (title, body)
$var title: $title
$var content_type: text/html
<div id="body">
$body
</div>
#结果为:
>>> out = render.page('hello', 'hello world')
>>> out.title
u'hello'
>>> out.content_type
u'text/html'
>>> str(out)
'\n\n<div>\nhello world\n</div>\n'
- default缺省情况下,web.py模板会请可能的使用HTML来解释文件,如果要去除这种效果,在$后面加一个'':''
- 如果要在模板文件中显示$需要用两个$$
- 十分混乱的python代码可以框在$code下
$code:
x = "you can write any python code here"
y = x.title()
z = len(x + y)
def limit(s, width=10):
"""limits a string to the given width"""
if len(s) >= width:
return s[:width] + "..."
else:
return s
And we are back to template.
The variables defined in the code block can be used here.
For example, $limit(x)
- builtins and globals:自行百度
- form类
- form类有Form和Input两个子类,一个用于初始化,一个用于构造
- Input子类的分类:
- Textbox - free form single line input (input type="text")
- Password - free form single line that hides input (input type="password")
- Textarea - free form multi line input (textarea)
- Dropdown - mutually exclusive input for lists (select and options)
- Radio - mutually exclusive input for a few options (input type="radio")
- Checkbox - binary input (input type="checkbox")
- Button - submit the form (button)
- 一个基本的登陆form看起来是这样:
login = form.Form(
form.Textbox('username'),
form.Password('password'),
form.Button('Login'),
)
#在需要使用的时候需要构造一个副本
f = login()
print f.render()
#结果:
<table>
<tr><th><label for="username">username</label></th><td><input type="text" id="username" name="username"/><div class="post" style="display: none;"></div></td></tr>
<tr><th><label for="password">password</label></th><td><input type="password" id="password" name="password"/><div class="post" style="display: none;"></div></td></tr>
<tr><th><label for="Login"></label></th><td><button id="Login" name="Login">Login</button><div class="post" style="display: none;"></div></td></tr>
</table>
#注意form定义时括号的位置,位置不当会导致定义失败
- Input类元素的属性
form.textbox("firstname",
form.notnull, #put validators first followed by optional attributes 因为validators为没有默认值的变量,应该放在有默认值变量的前面
class_="textEntry", #gives a class name to the text box -- note the underscore
pre="pre", #directly before the text box
post="post", #directly after the text box
description="please enter your name", #describes field, defaults to form name ("firstname")
value="bob", #default value
id="nameid", #specify the id
)
- 或者可以像html一样定义:
myform2 = form.Form(
form.textbox('phonenumber',
size="12",
maxlength="12" )
)
- Dropdown子类定义方式:
form.Dropdown('mydrop', [('value1', 'description1'), ('value2', 'description2')])
- validators用于检验输入的数据是否合乎标准,看例子:
signup = form.Form(
form.Textbox('username'),
form.Password('password'),
form.Password('password_again'),
validators = [form.Validator("Passwords didn't match.", lambda i: i.password == i.password_again)]
)
#或者:
myform = form.Form(
form.Textbox("boe"),
form.Textbox("bax",
form.notnull,
form.regexp('\d+', 'Must be a digit'),
form.Validator('Must be more than 5', lambda x:int(x)>5)),
form.Textarea('moe'),
form.Checkbox('curly'),
form.Dropdown('french', ['mustard', 'fries', 'wine']))
- form使用POST方法可以配合数据库使用:
def POST(self):
f = myform()
if f.validates():
web.insert('data_table', **f.d)
#don't do web.insert('data_table', **web.input()) because malicious data could be submitted too
else:
render.foo(f)
- 理解URL控制:
- 理解URL控制
- 问题: 如何为整个网站设计一个URL控制方案 / 调度模式
- 解决:
- web.py的URL控制模式是简单的、强大的、灵活的。在每个应用的最顶部,你通常会看到整个URL调度模式被定义在元组中:
- urls = (
- "/tasks/?", "signin",
- "/tasks/list", "listing",
- "/tasks/post", "post",
- "/tasks/chgpass", "chgpass",
- "/tasks/act", "actions",
- "/tasks/logout", "logout",
- "/tasks/signup", "signup"
- )
- 这些元组的格式是: URL路径, 处理类 这组定义有多少可以定义多少。如果你并不知道URL路径和处理类之间的关系,请在阅读cookbook之前先阅读Hello World example,或者快速入门。
- 路径匹配
- 你可以利用强大的正则表达式去设计更灵活的URL路径。比如 /(test1|test2) 可以捕捉 /test1 或 /test2。要理解这里的关键,匹配是依据URL路径的。比如下面的URL:
- http://localhost/myapp/greetings/hello?name=Joe
- 这个URL的路径是 /myapp/greetings/hello。web.py会在内部给URL路径加上^ 和$ ,这样 /tasks/ 不会匹配 /tasks/addnew。URL匹配依赖于“路径”,所以不能这样使用,如: /tasks/delete?name=(.+) ,?之后部分表示是“查询”,并不会被匹配。阅读URL组件的更多细节,请访问web.ctx。
- 捕捉参数
- 你可以捕捉URL的参数,然后用在处理类中:
- /users/list/(.+), "list_users"
- 在 list/后面的这块会被捕捉,然后作为参数被用在GET或POST:
- class list_users:
- def GET(self, name):
- return "Listing info about user: {0}".format(name)
- 你可以根据需要定义更多参数。同时要注意URL查询的参数(?后面的内容)也可以用web.input()取得。
- 开发子程序的时候注意
- 为了更好的控制大型web应用,web.py支持子程序。在为子程序设计URL模式的时候,记住取到的路径(web.ctx.path)是父应用剥离后的。比如,你在主程序定义了URL"/blog"跳转到'blog'子程序,那没在你blog子程序中所有URL都是以"/"开头的,而不是"/blog"。查看web.ctx取得更多信息。
- MORE:http://webpy.org/cookbook/index.zh-cn
阅读全文
0 0
- 如何从头搭建一个搜索引擎_简易的网页框架web.py
- 如何从头搭建一个搜索引擎_css的使用
- 如何快速搭建web.py开发框架?
- 如何从头搭建一个搜索引擎_pylucene,分词,语言编码问题
- 如何从头搭建一个搜索引擎_numpy以及openCV
- web.py搭建简易网站
- 如何从头搭建一个搜索引擎_HTML简介和BeautifulSoup的基础使用
- 一个web.py搭建的个人网站
- web.py等框架如何生成静态网页
- python的一个简易web程序index.py
- 在windows下如何快速搭建web.py开发框架
- 在windows下如何快速搭建web.py开发框架
- 搭建web.py开发框架
- Web.py 的搭建
- 如何从头搭建一个搜索引擎_HTTP请求,hash_table,bloomFilter和python并发编程
- 如何自己成功搭建一个SSM框架的WEB项目
- 如何自己成功搭建一个SSM框架的WEB项目
- python基于web.py的简易blog
- 利用 SRTP 模拟静多态
- 记一次面试编程题
- roseha,nec-ha以及mysql数据库热备思考
- GPX Data——GPX数据
- CJOJ 2171 火车站开饭店
- 如何从头搭建一个搜索引擎_简易的网页框架web.py
- 重启nginx后丢失nginx.pid,如何重新启动nginx
- # Android微信支付接入
- Android Studio平台下JNI开发:入门使用及常见错误
- network switch配置
- 服务器无法继续执行该事务,此会话中的活动事务已由另外一个会话提交或终止。
- python网络爬虫基础(利用HTMLParser)
- 搜索旋转排序数组I
- CSS之Flexbox制作CSS布局易如反掌