Python web frame:Bottle

s

s

http://bottlepy.org/docs/dev/

http://tocode.sinaapp.com/18


安装

Install the latest stable release via PyPi (easy_install -U bottle)


设置

设置Aptana Studio 3,不然找不到代码提示
Window -> Preferences -> Pydev -> Interpreter - Python

D:\Python27\Lib\site-packages\bottle-0.11.6-py2.7.egg

然后重启Aptana


简单的示例

# coding=utf-8

from bottle import route, run, template, static_file, abort, error, redirect, post, get, request

@route('/')
@route('/index.html')
def index():
    return '<a href="/hello/world">Go to Hello World page</a>'

@route('/hello')
@route('/hello/:name')
def hello(name='World'):
    return template('<b>Hello {{name}}</b>!', name=name)

@route('/static/:filename')
def serve_static(filename):
    return static_file(filename, root='./static')

@route('/raise_error')
def raise_error():
    abort(404, "error...")
    
@error(404)
def error404(error):
    return '404 error !!!!!'

@route('/redirect')
def redirect_to_hello():
    redirect('/hello')

@route('/ajax')
def ajax_response():
    return {'dictionary': 'you will see ajax response right? Content-Type will be "application/json"'}

@get('/upload')
def upload_view():
    return """
        <form action="/upload" method="post" enctype="multipart/form-data">
          <input type="text" name="name" />
          <input type="file" name="data" />
          <input type="submit" name="submit" value="upload now" />
        </form>
        """    


@post('/upload')
def do_upload():
    name = request.forms.get('name')
    data = request.files.get('data')
    if name is not None and data is not None:
        filename = data.filename
        print filename
        
        fullPath = u'./upfiles/'+filename                #中文文件名显示正常
#        outfile = file('./upfiles/'+filename, 'wb')     #中文文件名会是乱码
        
        outfile = file(fullPath, 'wb')
        try:
            buf = data.file.read(data.bufsize)
            currentSize = len(buf)
            print currentSize#测试window上buffer大小为8192字节,即8k
            while buf:
                outfile.write(buf)
                buf = data.file.read(data.bufsize)
                currentSize += len(buf)
            outfile.close()
            return "Hello %s! You uploaded %s (%d bytes)." % (name, filename, currentSize)
        except Exception, e:
            print e.message
            return 'Failed in uploading %s !' % filename
        
    return "You missed a field."

@route('/tpl')
def tpl():
    return template('test')
    
run(host='localhost', port=8080)


教程

译言网:Bottle 教程

http://article.yeeyan.org/view/35282/123019



s


s




s


你可能感兴趣的:(Python web frame:Bottle)