Bottle

最近在學習python,看同事用過bottle,本身也跟着看看,手冊有時候寫得不清楚,本身動手試試python

bottle.mount()的簡單代碼nginx

#!/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>"
相關文章
相關標籤/搜索