简述安装web.py框架及初次使用概述

 简述安装web.py框架及初次使用概述

分类: python学习记录   183人阅读  评论(0)  收藏  举报

前两天学习爬虫,感觉自己对网页的结构不够了解,不了解如何建站,其次就是正则表达式的应用很棘手,今天顺便花点时间了解了下web框架,初次学习使用,记录下重点内容,方便日后重新回顾和加深学习。

启程:

1、首先安装web.py

下载地址:

                  http://webpy.org/install

或者:右键打开链接 http://webpy.org/static/web.py-0.37.tar.gz  另存为就行了

将    web.py-0.37.tar.gz   解压到任意文件夹,然后在dos cmd命令行下进入该文件的setup.py 目录,执行python setup.py install    (当然已安装python)

c:\Python27\web>cd web.py-0.37

c:\Python27\web\web.py-0.37>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
creating build\lib\web
copying web\application.py -> build\lib\web
copying web\browser.py -> build\lib\web
copying web\db.py -> build\lib\web
copying web\debugerror.py -> build\lib\web
copying web\form.py -> build\lib\web
copying web\http.py -> build\lib\web
copying web\httpserver.py -> build\lib\web
copying web\net.py -> build\lib\web
copying web\python23.py -> build\lib\web
copying web\session.py -> build\lib\web
copying web\template.py -> build\lib\web
copying web\test.py -> build\lib\web
copying web\utils.py -> build\lib\web
copying web\webapi.py -> build\lib\web
copying web\webopenid.py -> build\lib\web
copying web\wsgi.py -> build\lib\web
copying web\__init__.py -> build\lib\web
creating build\lib\web\wsgiserver
copying web\wsgiserver\ssl_builtin.py -> build\lib\web\wsgiserver
copying web\wsgiserver\ssl_pyopenssl.py -> build\lib\web\wsgiserver
copying web\wsgiserver\__init__.py -> build\lib\web\wsgiserver
creating build\lib\web\contrib
copying web\contrib\template.py -> build\lib\web\contrib
copying web\contrib\__init__.py -> build\lib\web\contrib
running install_lib
creating C:\Python27\Lib\site-packages\web
copying build\lib\web\application.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\browser.py -> C:\Python27\Lib\site-packages\web
creating C:\Python27\Lib\site-packages\web\contrib
copying build\lib\web\contrib\template.py -> C:\Python27\Lib\site-packages\web\c
ontrib
copying build\lib\web\contrib\__init__.py -> C:\Python27\Lib\site-packages\web\c
ontrib
copying build\lib\web\db.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\debugerror.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\form.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\http.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\httpserver.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\net.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\python23.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\session.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\template.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\test.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\utils.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\webapi.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\webopenid.py -> C:\Python27\Lib\site-packages\web
copying build\lib\web\wsgi.py -> C:\Python27\Lib\site-packages\web
creating C:\Python27\Lib\site-packages\web\wsgiserver
copying build\lib\web\wsgiserver\ssl_builtin.py -> C:\Python27\Lib\site-packages
\web\wsgiserver
copying build\lib\web\wsgiserver\ssl_pyopenssl.py -> C:\Python27\Lib\site-packag
es\web\wsgiserver
copying build\lib\web\wsgiserver\__init__.py -> C:\Python27\Lib\site-packages\we
b\wsgiserver
copying build\lib\web\__init__.py -> C:\Python27\Lib\site-packages\web
byte-compiling C:\Python27\Lib\site-packages\web\application.py to application.p
yc
byte-compiling C:\Python27\Lib\site-packages\web\browser.py to browser.pyc
byte-compiling C:\Python27\Lib\site-packages\web\contrib\template.py to template
.pyc
byte-compiling C:\Python27\Lib\site-packages\web\contrib\__init__.py to __init__
.pyc
byte-compiling C:\Python27\Lib\site-packages\web\db.py to db.pyc
byte-compiling C:\Python27\Lib\site-packages\web\debugerror.py to debugerror.pyc

byte-compiling C:\Python27\Lib\site-packages\web\form.py to form.pyc
byte-compiling C:\Python27\Lib\site-packages\web\http.py to http.pyc
byte-compiling C:\Python27\Lib\site-packages\web\httpserver.py to httpserver.pyc

byte-compiling C:\Python27\Lib\site-packages\web\net.py to net.pyc
byte-compiling C:\Python27\Lib\site-packages\web\python23.py to python23.pyc
byte-compiling C:\Python27\Lib\site-packages\web\session.py to session.pyc
byte-compiling C:\Python27\Lib\site-packages\web\template.py to template.pyc
byte-compiling C:\Python27\Lib\site-packages\web\test.py to test.pyc
byte-compiling C:\Python27\Lib\site-packages\web\utils.py to utils.pyc
byte-compiling C:\Python27\Lib\site-packages\web\webapi.py to webapi.pyc
byte-compiling C:\Python27\Lib\site-packages\web\webopenid.py to webopenid.pyc
byte-compiling C:\Python27\Lib\site-packages\web\wsgi.py to wsgi.pyc
byte-compiling C:\Python27\Lib\site-packages\web\wsgiserver\ssl_builtin.py to ss
l_builtin.pyc
byte-compiling C:\Python27\Lib\site-packages\web\wsgiserver\ssl_pyopenssl.py to
ssl_pyopenssl.pyc
byte-compiling C:\Python27\Lib\site-packages\web\wsgiserver\__init__.py to __ini
t__.pyc
byte-compiling C:\Python27\Lib\site-packages\web\__init__.py to __init__.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\web.py-0.37-py2.7.egg-info

c:\Python27\web\web.py-0.37>python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import web
>>>

在python 命令行中输入 import web ,若无报错,则说明安装完成;

按照官方给出的用例如下,并进行分析:

 

[python]  view plain copy
  1. #!/usr/bin/env python    
  2. # -*- coding:utf-8 -*-    
  3. import web  
  4.   
  5. urls = (  
  6.     '/''index'  
  7. )  
  8.   
  9. class index:  
  10.     def GET(self):  
  11.         return "Hello, world!"  
  12.   
  13. if __name__ == "__main__":  
  14.     app = web.application(urls, globals())  
  15.     app.run()  
>>> 
http://0.0.0.0:8080/
127.0.0.1:57027 - - [30/Aug/2012 13:49:14] "HTTP/1.1 GET /" - 200 OK        地址栏中实际输入的地址:http://127.0.0.1:8080/ 才会打印出hello,world!                  ( http://localhost:8080/,根据localhost确定)

 

代码分析:

1、先导入web.py模块;

2、web.py 处理 URL的组织方式;

urls = (
    '/', 'index'
)

这行表示我们要URL/(首页)被一个叫index的类处理

第一部分是匹配URL的正则表达式,像//help/faq/item/(\d+)等(\d+将匹配数字)。圆括号表示捕捉对应的数据以便后面使用。第二部分是接受请求的类名称,像indexviewwelcomes.hello (welcomes模块的hello类),或者get_\1\1 会被正则表达式捕捉到的内容替换,剩下来捕捉的的内容将被传递到你的函数中去

3、让web.py去创建一个基于提交的URL列表的application。这个application会在这个文件的全局命名空间中查找对应类

app = web.application(urls, globals()

4、GET是用于请求网页文本。当你在浏览器输入harvard.edu,它会直接访问Harvard的web服务器,去GET /。 第二个最有名的是POST,它经常被用在提交form,比如请求买什么东西。每当提交一个去做什么事情(像使用信用卡处理一笔交易)的请求时,你可以使用POST。这是关键,因为GET的URL可以被搜索引擎索引,并通过搜索引擎访问。虽然大部分页面你希望被索引,但是少数类似订单处理的页面你是不希望被索引的 (想象一下Google尝试去购买你网站上的所有东西)。

class index:
    def GET(self):
        return "Hello, world!"

web.py的代码中,这两个方法有明确区分;

5、启动上面写的应用

if __name__ == "__main__":

          app.run()

6、最后 python code.py 或在python shell里面直接运行;

模板:

模板新建一个目录(命名为 templates),在该目录下新建一个以 .html 结尾的文件,内容如下:

 

$def with (name)  $if name:     I just wanted to say <em>hello</em> to $name. $else:     <em>Hello</em>, world!

 

如上,该模板看起来就像 python 文件一样,除了顶部的 def with (表示该模板将从这后面取值)和总是位于代码段之前的$。当前,template.py 首先请求模板文件的首行$def 。当然,你要注意 web.py 将会转义任何任何用到的变量,所以当你将 name 的值设为是一段 HTML 时,它会被转义显示成纯文本。如果要关闭该选项,可以写成$:name 来代替 $name

修改code.py文件内容如下:

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
import web

render = web.template.render(r'F:\\templates\\')
urls = (
    '/', 'index'
)

class index:
    def GET(self):
        name = 'Bob'    
        return render.index(name)

        #return "Hello, world!"

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

'index' 是模板的名字,'name' 是传入模板的一个参数

访问站点它将显示:

注:

以上过程中比较容易出错的就是,在windows环境下操作时,templates目录的表示方法为:(r'F:\\templates\\'),代码和index文件都放在该目录下就行了;建立的index.html文件只要后缀名正确就行,其默认的打开方式无关(若有报错的话);

如果我们想让用户自行输入他的名字,则修改代码如下:

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
import web

render = web.template.render(r'F:\\templates\\')
urls = (
    '/', 'index'
)

class index:
    def GET(self):
        i = web.input(name=None)
        return render.index(i.name)

        ##name = 'Bob'    
        ##return render.index(name)
        #return "Hello, world!"

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

访问 / 将显示 hello world,访问 /?name=Joe 将显示 I just wanted to sayhello to Joe.

修改下 URL 配置,修改下 index.GET,代码如下:

#!/usr/bin/env python  
# -*- coding:utf-8 -*-  
import web

render = web.template.render(r'F:\\templates\\')
urls = (
    '/(.*)', 'index'
)

class index:
    def GET(self,name):
        #i = web.input(name=None)
        return render.index(name)
        ##name = 'Bob'    
        ##return render.index(name)
        #return "Hello, world!"

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

访问 /joe 看看,它会显示如下:

根据/ 后输入的不同,也可以显示其他的文字,如:

输入:/it is a test

如果学习更多关于 web.py 的模板处理,请访问 web.py 模板.


数据库的知识暂时没有学到,不在这里探究了!

你可能感兴趣的:(框架,python,安装,web.py)