文档介绍:webpy使用笔记(一)
webpy入门
工作环境中需要经常生产和测试服务器,机房一直很混乱,因此萌生了开发一个简单方便的服务器管理系统(说的好高大上,其实就是个可以获取服务器信息的小web应用)。之所以选择webpy,正式因为它够简单,尤其是对于我这种python新人来说。它是一款轻量级的python web开发框架,对于个人开发小应用来说很适合。
webpy install
下载:wget -
安装:python install
webpy 'hello world'
可以参考webpy的官方文档:/tutorial
hello, world如下:
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
return "Hello, world!"
if __name__ == "__main__":
app = (urls, globals())
()
在webpy中,url请求的映射在urls元组中,如上图中GET ip:port/,会直接调用index类的GET方法,返回字符串'hello, world!';
class index中包含了一个GET方法,用来处理与index相应的url的GET请求的;
在主函数中,只需要创建一个application对象,运行就可以开启一个简单的web应用,默认的地址为::8080
GET && POST
web包含两种方法:GET和POST
对于GET,可以采用:
class index:
def GET(self):
return "Hello, world!"
而,对于POST,采用:
class index:
def POST(self):
data = (name=None)
return "Hello, " + + "!"
html模板
在webpy中,一般采用templates来存放html页面文件。大概的访问方式如下:
urls = (
'/img', 'image'
)
render = ('templates')
class image:
def GET(self):
return ()
urls中定义了url映射,访问ip:port/img会直接条用class image来处理;
(path)是用来指定存放html的目录,上面指定了html的指定存放位置位于当前文件夹下的tem