大家好,今天为大家介绍一款非常轻量级的的web开发框架,karloop框架。使用python开发
首先我们下载karloop源码进行安装。
下载成功后解压,进入解压后的路径,进入终端,运行命令:sudo python setup.py install
如果是window系统,则打开cmd,运行:python setup.py install
我们按照如下路径创建一个项目:
├── base.py
├── config.py
├── handlers
│ ├── __init__.py
│ ├── handlers.py
│ └── pages.py
├── index.py
├── static
│ ├── js
│ │ ├── jquery-1.11.3.js
│ │ └── request.js
│ └── media
│ ├── 1.mp3
│ ├── 1.mp4
│ ├── 2.mp3
│ ├── 3.mp3
│ ├── 4.mp3
│ └── 5.mp3
└── template
├── main.html
└── media_list.html
首先我们来写一个base.py,所有的handler都可以继承,代码如下:
# coding=utf-8 __author__ = 'karl' import json from karloop.KarlBaseResponse import BaseResponse class BaseHandler(BaseResponse): def response_as_json(self, body): self.set_header('application/json; charset="utf-8"') response_data = json.dumps(body) return self.response(response_data)
然后写config.py,代码如下:
# coding=utf-8 __author__ = 'karl' import os settings = { "static": os.path.join(os.path.dirname(__file__), "static/"), "template": os.path.join(os.path.dirname(__file__), "template/"), "cookie_code": "85701729" }
再来个index.py
# coding=utf-8 __author__ = 'karl' from config import settings from karloop.KarlBaseApplication import BaseApplication from handlers.pages import MainPage, MediaPage from handlers.handlers import ListAllMediaHandler from karloop.parse_command import parse_command_line handlers = { "/main": MainPage, "/media": ListAllMediaHandler, "/media_page": MediaPage } class MediaWebApplication(BaseApplication): def __init__(self): super(MediaWebApplication, self).__init__( handlers=handlers, settings=settings ) if __name__ == '__main__': media_web_application = MediaWebApplication() parse_command_line(application=media_web_application, default=8888) media_web_application.run()
上handlers.py的代码:
# coding=utf-8 __author__ = 'karl' from base import BaseHandler class ListAllMediaHandler(BaseHandler): def get(self): extension = self.get_argument("extension", "") if not extension: return self.response_as_json( { "error": "no valid parameter" } ) extension = extension.lower() media_dict = { "mp3": ["1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3"], "mp4": ["1.mp4", "2.mp4", "3.mp4"] } if extension not in media_dict.keys(): return self.response_as_json( { "error": "no data" } ) response_data = media_dict[extension] return self.response_as_json(response_data)
pages.py的代码
# coding=utf-8 __author__ = 'karl' from karloop.KarlBaseResponse import BaseResponse class MainPage(BaseResponse): def get(self): dictionary = { "title": "Main Page" } main_string = "Welcome!" test_list = ["This is a demo.", "Developed use karloop."] return self.render( "main.html", { "test_dic": dictionary, "test_string": main_string, "test_list": test_list } ) class MediaPage(BaseResponse): def get(self): return self.render("media_list.html")
整个项目的python 代码就这些,总体很简单,毕竟只是一个demo吗,项目的代码下载地址:点击
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-13 14:17:54