Bottle GET method. Request

python

bottle framework

#!/usr/bin/python
# -*- coding utf-8 -*-

from bottle import route, run, debug, request
#from cgi import escape

@route('/hello', method='GET')
def hello():
    name = request.GET.get('name')
    if not name:
        name = "This guy's unknow :("
    return 'Hello {0}'.format(name)

debug(True)
run(host='localhost', port=8080, reloader=True)

Now I add a route here which is the

'/hello'

and I define the method to be 'GET'

 

And the request has         request.GET.get('name')

then

name will be assigned again

 

All the def will return

return 'hello {0}'.format(name)

The reloader means the server will restart when it finds any files has been changed.

 

如果有注释掉前面的那句的话。

#from cgi import escape

可以出现下面的结果:

 

现在我们加上这个cgi的escape,目的是为了不让浏览器地址栏里的内容自动检索内容。

<h1></h1>标签

让我们大家来看看效果

 

看到了地址栏里面就没有继续在parse html代码了。

看看html代码内部的源码是:

非解析的html代码,这里曾经学习过php,跟这个比较相似。一样的道理。 &lt;     &gt;  标签。

 

And if the method is POST

check those codes out:

#!/usr/bin/python
# -*- coding utf-8 -*-

from bottle import route, run, debug, request
#from cgi import escape

@route('/hello', method='POST')
def hello():
    name = request.POST.get('name')
    if not name:
        name = "This guy's unknow :("
    return 'Hello {0}'.format(name)

debug(True)
run(host='localhost', port=8080, reloader=True)

 

实际上POST与GET没有什么区别。

只是在判断上有区别,判断一下,post可以让服务器修改一些内容,如果是get的话,那就是向服务器取得内容。就这么简单。

 

posted @ 2013-06-24 17:45  spaceship9  阅读(603)  评论(0编辑  收藏  举报