I am new to both GAE and PiCould, and have some basic questions when I would like to call a published function from GAE. It seems like my approach of calling a published function is not recognized. So can you give me some suggestions?
Thanks for your help!
UPDATE: I am not sure why people think this question is not valuable. My standing point is that since GAE only accept pure-Python file, it is necessary to find a way for models written in other langurages (i.e., Fortran77). Thus, some non-Python programs can be uploaded to other cloud servers such like PiCloud and then called by GAE. With people’s help (thank you!) I have figured out this issue. I have attached my codes below for other’s reference:
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()
The line should be: