Main.js
Parse.Cloud.job("grabPrices", function(request, status) {
// Set up to modify user data
Parse.Cloud.httpRequest({
method: 'POST',
url: 'http://xxx.parseapp.com/xxx.py',
success: function(httpResponse) {
console.log(httpResponse.text);
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
}
})
});
Main.js是我想要运行的Parse Cloud Job,它基本上访问python脚本并运行它。这可能吗?如果是这样,我在代码中做了哪些更改?
答案 0 :(得分:2)
我使用Heroku解决了我的问题。
我的代码类似于@AlexCoren的答案,如下所示:
Parse.Cloud.job("grabPrices", function(request, status) {
// Set up to modify user data
Parse.Cloud.httpRequest({
url: 'https://xxx.herokuapp.com/',
success: function(httpResponse) {
console.log(httpResponse.text);
status.success("Pricelist grabbed successfully!");
},
error: function(httpResponse) {
console.error('Request failed with response code ' + httpResponse.status);
status.success("Oops.... something went wrong.");
}
})
});
对于heroku,我只是略微修改了views.py
文件(我只是按照教程'开始使用python',下载了示例项目并修改了\python-getting-started\hello\views.py
文件):
from django.shortcuts import render
from django.http import HttpResponse
import urllib2
import urllib
#import requests
import json, httplib
import time
from datetime import datetime
from .models import Greeting
# Create your views here.
def index(request):
# I put my python code here so it will run at the url `https://xxx.herokuapp.com/'
答案 1 :(得分:1)
我在其中一个应用中做了类似的事情。我在Heroku上托管了它。代码如下:
Parse.Cloud.define('MyFunction', function(request, response) {
var someParam = request.params['SOME_PARAM'];
var url = 'http://myapp.herokuapp.com/?param=' + someParam;
Parse.Cloud.httpRequest({
url: url,
success: function(httpResponse) {
console.log(httpResponse.headers);
console.log(httpResponse.text);
response.success();
}, error: function(httpResponse) {
response.error('Uh oh!');
}
});
});
该url指定一个参数,该参数将被发送到我的服务器,然后在我的python脚本中用作参数。
答案 2 :(得分:0)
不幸的是,这是不可能的。 Parse后端只执行javascript文件(云代码文件夹)或html,css和javascript文件(公共文件夹)。
即使您能够将python文件上传到其中一个文件夹,您也希望能够执行该文件。
你可以将你的python文件上传到你的公共文件夹,你可以有一个URL(mysubdomain.parseapp.com/python_file.py),但如果你使用Parse.Cloud.httpRequest,你将获得内容对于该文件,服务器不会执行该文件。
例如,如果您的python文件具有以下代码:
print "Hello World!"
您在httpRequest上获得的结果将是:
print "Hello World!"
而不是:
Hello World!
这就是你想要的。
python文件应该托管在执行python的其他服务器上。
我希望这是有道理的。 哈维尔。