Use Karloop to develop a demo of a media site

Source: Internet
Author: User

Hello everyone, today we introduce a very lightweight web development framework, Karloop framework. Developing with Python

First we download Karloop source for installation.

Source Address

After the download is successfully unzipped, enter the extracted path, enter the terminal, Run command: sudo python setup.py install

If it is a window system, open cmd, run: Python setup.py install

We create a project according to the following path:

├──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

First we write a base.py, all the handler can inherit, the code is as follows:

# coding=utf-8__author__ = ' karl ' Import Jsonfrom karloop. Karlbaseresponse Import Baseresponseclass 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)
Then write config.py, code as follows:

# coding=utf-8__author__ = ' karl ' Import ossettings = {    "static": Os.path.join (Os.path.dirname (__file__), "static/" ),    "template": Os.path.join (Os.path.dirname (__file__), "template/"),    "Cookie_code": "85701729"}

One more index.py.

# coding=utf-8__author__ = ' karl ' from config import settingsfrom karloop. Karlbaseapplication Import baseapplicationfrom handlers.pages import MainPage, mediapagefrom handlers.handlers Import Listallmediahandlerfrom Karloop.parse_command Import parse_command_linehandlers = {    "/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 ()

The Code on handlers.py:

# coding=utf-8__author__ = ' Karl ' from the base import Basehandlerclass 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's Code
# coding=utf-8__author__ = ' Karl ' from Karloop. Karlbaseresponse Import Baseresponseclass MainPage (baseresponse):    def get (self):        dictionary = {            "title": "Main page"        }        main_string = "welcome!"        Test_list = ["This was 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 ")
The whole project Python code on these, the overall is simple, after all is just a demo, Project code: click

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Karloop to develop a demo of a media site

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.