Giter Club home page Giter Club logo

miniweb's Introduction

DISCARDED

MiniWeb

A simple Web Server Framework based on WSGI.

class Web(pi = "" , port = 8000)

Class of web server,default port is 8000.

# create a server listen to 2333 port
web  = Web(port=2333)

GET(self,url)

Decorator to bind url and function in GET method.

# call index() while visit '/' by GET metohd
@web.GET('/')
def index(request,response) :
    pass

POST(self,url)

Decorator to bind url and function in POST method.

# call login() while visit '/login' by POST method
@web.POST('/login')
def login(request,response) :
    pass

getKeys(self)

return all keys(form name) of GET parameters.

# get keys list of GET
keys = web.getKeys()

postKeys(self)

return all keys(form name) of POST parameters.

# get key list of POST
keys = web.postKeys()

getParam(self,key)

return the value of GET parameter index by key(form name).

# get value of GET
id = web.getParam('id')

postParam(self,key)

return the value of POST parameter index by key(form name).

# get value of POST
username = web.postParam('username')

filename(self,key)

return the filename of POST upload index by key(form name).

# get file name of POST
filename = web.filename('picture')

file(self,key)

return a file-like object of POST upload index by key(form name).

# get file of POST
file = web.file('picture')

exec(self)

execute server.

web.exec()

class Template()

Class of template, template variable in HTML file is <% name %>.

# create a Template to render HTML
template = Template()

openHtml(self,path)

read a HTML file

# open a HTML file
template.openHtml('index.html')

render(self,**cmd)

render the HTML data

# change <% value %> to Welcome
template.render(value =  'Welcome')

html(self)

return the rendered HTML data

html = template.html()

Example

HTML file index.html

<html>
    <head>
        <meta charset='utf-8'/>
        <title><% title %></title>
    </head>
    <body>
        <p><% article %></p>
    </body>
</html>

Python file

#! /usr/bin/env python3
import MiniWeb

web = MiniWeb.Web() # create web server,listen to default port 8000

@web.GET('/')
def index(request,response) :
    t = MiniWeb.Template()
    response('200 ok',[('Content-Type','text/html')])
    t.openHtml('index.html')
    t.render(title = 'Home Page')
    t.render(article = 'Welcome')
    return t.html()
    
web.exec()

Visit localhost:8000 in broswer.

miniweb's People

Contributors

hubenchang0515 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.