在 main.py 代码中:
# -*- coding: utf-8 -*-
import os
import time
import re
import urllib
import urllib2
import MySQLdb
import webapp2
import jinja2
template_dir = os.path.join(os.path.dirname(__file__), 'templates')
jinja_env = jinja2.Environment(loader = jinja2.FileSystemLoader(template_dir),
autoescape = True)
class Handler(webapp2.RequestHandler):
def write(self, *a, **kw):
self.response.out.write(*a, **kw)
def render_str(self, template, **params):
t = jinja_env.get_template(template)
return t.render(params)
def render(self, template, **kw):
self.write(self.render_str(template, **kw))
class MainPage(Handler):
def get(self):
self.render('front.html')
def post(self):
hostip = self.request.get('hostip')
username = self.request.get('username')
dbname = self.request.get('dbname')
password = self.request.get('password')
if hostip and username and dbname and password:
conn = MySQLdb.connect(host = hostip, user = username, passwd = password, db = dbname)
cur = conn.cursor()
cur.execute("show tables")
alltable = cur.fetchall()
tablenames = []
for i in range(len(alltable)):
for j in range(len(alltable[i])):
tablenames.append(alltable[i][j])
finalname = tablenames
app = webapp2.WSGIApplication([('/', MainPage)],
debug = True)
def main():
from paste import httpserver
httpserver.serve(app, host = '127.0.0.1', port = '8888')
if __name__ == '__main__':
main()
在 front.html 中:
<html>
<body>
<h1>ACCESS YOUR SQL</h1>
<form method = "post" accept-charset="UTF-8" action="/"/>
<label>
<div>SQL Host IP</div>
<input type="text" name="hosttip" value="{{hostip}}">
</label>
<label>
<div>UserName</div>
<input type="text" name="username" value="{{username}}">
</label>
<label>
<div>DBname</div>
<input type="text" name="dbname" value="{{dbname}}">
</label>
<label>
<div>Password</div>
<input type="password" name="password" value="{{password}}">
</label>
<input type="submit" value="submit">
<form>
<hr>
{% for tablename in finalname %}
<form>
<input type="checkbox" name="tablename">{{tablename}}
</form>
{% endfor %}
</body>
</html>
填充并运行后,front.html什么都没有返回,无法返回fetchall的数据,代码有什么问题,有人可以帮我解决吗?非常非常非常感谢:(