我正在尝试使用Google App Engine创建一个简单的Web应用程序。我使用jinja2来渲染前端html文件。用户输入其AWS凭据并获取区域的输出并与其连接虚拟机。 我有一个控制器文件,我导入一个模型文件,它看起来像这样:
import webapp2
import jinja2
import os
import model
jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
class MainPage(webapp2.RequestHandler):
def get(self):
template = jinja_environment.get_template('index.html')
self.response.out.write(template.render())
def request_a(self):
a = self.reguest.get('a')
return a
def request_b(self):
b = self.reguest.get('b')
return b
class Responce(webapp2.RequestHandler):
def get(self):
self.response.headers['Content-Type'] = 'text/plain'
self.response.write(testing_ec2.me.get_machines())
app = webapp2.WSGIApplication([('/2', MainPage), ('/responce', Responce)], debug=True)
然后我有一个模型文件,我导入控制器文件,它看起来像这样:
import boto.ec2
import controller
import sys
if not boto.config.has_section('Boto'):
boto.config.add_section('Boto')
boto.config.set('Boto', 'https_validate_certificates', 'False')
a = controller.MainPage.get()
b = controller.MainPage.get()
class VM(object):
def __init__(self, a, b):
self.a = a
self.b = b
self.regions = boto.ec2.regions(aws_access_key_id = a, aws_secret_access_key = b)
def get_machines(self):
self.region_to_instance = {}#dictionary, which matches regions and instances for this region
for region in self.regions:
conn = region.connect(aws_access_key_id = self.a, aws_secret_access_key = self.b)
reservations = conn.get_all_instances()#get reservations(need understand them better)
if len(reservations) > 0:#if there are reservations in this region
self.instances = [i for r in reservations for i in r.instances]#creates a list of instances for that region
self.region_to_instance[region.name] = self.instances
return self.region_to_instance
me = VM(a, b)
me.get_machines()
当我运行它时,它会抛出一个错误:类型对象'MainPage'没有属性'request_a' 我假设它发生了,因为我不调用MainPage类的实例而是调用类本身。什么是MainPage(以及它的父webapp.RequestHandler)类的实例?我如何在另一个模块中调用它?
答案 0 :(得分:0)
你的代码对我来说很奇怪。我不明白你的编码习惯。
一般答案是:如果您想使用MainPage的方法,可以使用继承。
但是。如果我理解你的代码的目标。为什么不从您的Responce类调用boto。但是,在这里你使用get,你应该使用帖子,因为你发布了一个带有AWS凭证的表单。
所以我建议:
使用get和post方法创建一个MainPage来处理表单
发出boto请求并将结果与jinja一起发送给用户。
另请参阅GAE Python27入门和处理表单: https://developers.google.com/appengine/docs/python/gettingstartedpython27/handlingforms?hl=nl