美文网首页视觉艺术
Python Web框架-webpy

Python Web框架-webpy

作者: 橘猫吃不胖 | 来源:发表于2020-04-13 22:11 被阅读0次

被同事推荐了一个web框架,大概看了下,看上去很简单,这里记录下学习。

web.py is a web framework for Python that is as simple as it is powerful

什么是Web框架

Web应用框架(Web application framework)是一种开发框架,用来支持动态网站、网络应用程序及网络服务的开发。其类型有基于请求的和基于组件的两种框架

百度百科是这么说的,就是这么个意思

为什么想到看这个呢?
主要是下午提到说要是有个XX接口就好了,同事说用webpy写一个很简单的,于是就来看看看。

webpy

官网:https://webpy.org/
还有中文版教程,直接上手:https://webpy.org/docs/0.3/tutorial.zh-cn

直接pip安装就行

pip install web.py

使用conda的话,得这样:

conda install -c conda-forge web.py

好了,来一个小栗子
就写一个hello world好了
需求:
我们访问一个地址,然后返回hello world

遇到一个问题:No module named 'cheroot'
装一下先

conda install cheroot

实例:

class hello:
    def GET(self):
        return 'hello world!'
    
import web

urls = (
    '/','hello'
)

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

这几行代码,就实现了我们上面的需求


访问本地的8080端口,就看到了hello world !

注意下,这里要在命令行里去调用,Spyder里貌似会直接结束程序

这里需要我们做的就是写一个class,实现以下GET方法,GET就是接收GET请求,还有POST请求,我们后面再说;
另一个就是定义监听的URL了,其他都是标准代码,直接用就好

相比Java来说,简单多了

接收一个参数

现在我们想不同的人访问,可以显示不同人的名字出来,也就是传一个参数过去
我们直接使用web.input就可以获取到

class hello:
    def GET(self):
        i = web.input()
        print(i)
        return 'hello world!'

import web

urls = (
    '/','hello'
)

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

这里我们访问:http://localhost:8080/?name=lufei&age=20&gender=male
我们加了几个参数,我们看看后台的输出:

function input(*requireds, **defaults)
Returns a storage object with the GET and POST arguments.
A Storage object is like a dictionary except obj.foo can be used in addition to obj['foo'].

知道了接收参数,我们再改一下代码:

class hello:
    def GET(self):
        i = web.input()
        return 'hello {0} , I know you age={1} and gender={2}'.format(i['name'] , i['age'] , i['gender'])
  

访问地址:http://localhost:8080/?name=lufei&age=20&gender=male

好玩儿了吧,哈哈哈,简单又好用,不错

模板

说到Web框架了,肯定会想到HTML,为了更好的可视化效果,HTML是不可缺少的,在webpy中使用HTML也很简单

这里要用户模板的渲染

import web

render = web.template.render('templates/')

urls = (
    '/','hello'
)

class hello:
    def GET(self):
        return render.hello()
 

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

我们在当前目录,新增一个templates目录,新增了一个hello.html文件

内容如下:

<h1>哈哈哈哈</h1>
<h3>今天、明天、昨天</h3>

<em>Hello</em>, world!

然后我们再访问一下:http://localhost:8080

不错,主要两行代码:
指定了我们的模板目录

render = web.template.render('templates/')

然后,调用模板:
这个hello就是模板的名字

return render.hello()

模板也是可以接收参数的

模板报错问题:

刚才改了好几次代码,一直报这个错误,不知道为啥,后来才发现,是
$def with (name)
必须写在第一行,一定要写在第一行!!!


我们回来看看代码:

import web

render = web.template.render('templates/')

urls = (
    '/','hello'
)

class hello:
    def GET(self):
        i = web.input()
        return render.hello(i['name'])
 

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

主要是HTML文件,注意哦
HTML中可以写python代码,注意格式

$def with (name)

<h1>哈哈哈哈</h1>
<h3>今天、明天、昨天</h3>

<em>Hello</em>, world!

<hr/>
<hr/>


Hello , ${name}

http://localhost:8080/?name=lufei

好了,先到这里,又掌握一个小知识,明天再继续学习。

相关文章

网友评论

    本文标题:Python Web框架-webpy

    本文链接:https://www.haomeiwen.com/subject/jwtomhtx.html