从Google App Engine调用PiCloud功能时出现问题

时间:2012-03-26 19:51:09

标签: python google-app-engine

我是GAE和PiCould的新手,当我想从GAE调用已发布的函数时,我有一些基本的问题。看来我调用已发布函数的方法无法识别。你能给我一些建议吗?

感谢您的帮助!

更新:我不确定为什么人们认为这个问题没有价值。我的观点是,由于GAE只接受纯Python文件,因此有必要为其他语言编写的模型(即Fortran77)找到一种方法。因此,一些非Python程序可以上传到其他云服务器,如PiCloud,然后由GAE调用。在人们的帮助下(谢谢!)我已经找到了这个问题。我已在下面附上我的代码供其他人参考:

import os
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from google.appengine.ext.webapp.util import run_wsgi_app
import webapp2 as webapp
import json
import base64
#import urllib2
import urllib

from google.appengine.api import urlfetch

api_key='1111'
api_secretkey='adsad'

####define and publish a function######
def square(x):
    """Returns square of a number"""
    print 'Squaring %d' % x
    return x*x

cloud.setkey(api_key, api_secretkey)
cloud.rest.publish(square, "square_func")

url = 'https://' + 'api.picloud.com/r/3303/square_func' 
input_val=22

#######call the function################# 
base64string = base64.encodestring('%s:%s' % (api_key, api_secretkey))[:-1]
http_headers = {'Authorization' : 'Basic %s' % base64string}
data = urllib.urlencode({"x":input_val})
response = urlfetch.fetch(url=url, payload=data, method=urlfetch.POST, headers=http_headers)

jid= json.loads(response.content)['jid']
output_st = 'queued'
#
while output_st=="queued":
    response_st = urlfetch.fetch(url='https://api.picloud.com/job/?jids=%s&field=status' %jid, headers=http_headers)
    output_st = str(json.loads(response_st.content)['info']['%s' %jid]['status'])

url_val = 'https://api.picloud.com/job/result/?jid='+str(jid)
response_val = urlfetch.fetch(url=url_val, method=urlfetch.GET, headers=http_headers)
output_val = json.loads(response_val.content)['result']


class Page(webapp.RequestHandler):
    def get(self):
        html = """<table width="600" border="1">
                          <tr>
                            <th width="480" scope="col">Outputs</div></th>
                            <th width="120" scope="col">Value</div></th>                            
                          </tr>
                          <tr>
                            <td>Input</td>
                            <td>%s</td></tr>                          
                          <tr>
                            <td>picloud jid</td>
                            <td>%s</td></tr>
                          <tr>
                            <td>picloud status</td>
                            <td>%s</td></tr>
                          <tr>
                            <td>picloud results</td>
                            <td>%s</td></tr>                             
                         </table>"""%(input_val, jid, output_st, output_val)

        self.response.out.write(html)

app = webapp.WSGIApplication([('/.*', Page)], debug=True)

def main():
    run_wsgi_app(app)

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:2)

该行应为:

output_val = json.loads(response.content)