最近在学习python,看同事用过bottle,自己也跟着看看,手册有时候写得不清楚,自己动手试试
bottle.mount()的简单代码
#!/usr/bin/env python
#this is main application in main.py
from bottle import route, run, Bottle, static_file, request
import login
import login_new
app = Bottle()
#app.mount('/login', login.login)
app.mount('/login', login_new.logins) #把“/login”导入到login_new中处理,logins时个Bottle
@app.route('/hello')
@app.route('/hello/<name>')
def index(name='World'):
return '<b>Hello %s!<b>' % name
@app.route('/img')
def get_img():
return static_file('t-1.jpg', '/home/liukai/tmp/nginx/files')
@app.route('/download')
def get_download():
filename = 't-1.jpg'
return static_file(filename, '/home/liukai/tmp/nginx/files', download=filename)
if __name__=='__main__':
run(host='localhost', port=8080)
else:
application = app
#!/usr/bin/env python
#this is sub-app in login_new.py
from bottle import Bottle, request
logins = Bottle()
@logins.route('/')
def login_get():
return '''
<form action="/login" method="post">
Username: <input name="username" type="text" />
Password: <input name="password" type="password" />
<input value="Login" type="submit" />
</form>
'''
def print_dict(rdict):
for key in rdict.keys():
print("key=%s, value=%s" % (key,rdict[key]))
@logins.route('/', method='POST')
def do_login():
#print_dict(env)
username = request.forms.get('username')
password = request.forms.get('password')
if True:#check_login(username, password):
return "<p>%s login information was correct.</p>" % username
else:
return "<p>Login failed.</p>"