在Heroku上运行Python网络服务
我有一个用Python和Flask写的网页服务,在我的本地服务器上运行得很好。现在我把它部署到了Heroku上,但当我尝试从Heroku访问时,它显示应用程序有一些错误,无法运行。我在本地测试过(确认网页服务运行得很好),然后才把它部署到Heroku。我使用了一个shelve文件,网页服务从中获取持久数据。在本地运行时一切正常,但我觉得在Heroku上,它无法获取我的shelve文件的内容。在我之前部署到Heroku的网页服务中,我每次运行时都会创建一个新的shelve文件,但在这个网页服务中,我已经创建了一个包含持久数据的shelve文件,我的网页服务只是引用这些数据,而不往里面写任何东西。
这是我的网页服务脚本:
news_index=shelve.open('IndexedMapping')
item = [] # list for storing the final results
with open('TodaysToiScrapedItemsOutput.json') as f: #load json file
data = json.load(f)
input_headline = news_string
input_headline_list = input_headline.split()
temp_input_headline_list = []
for each_word in input_headline_list:
temp_input_headline_list.append(each_word)
for each_word in temp_input_headline_list:
if (each_word.lower() in ignore_this_words):
input_headline_list.remove(each_word)
hit_cnt=0
key_and_hit_cnt_dict={}
for each_key in news_index:
for each_word in input_headline_list:
if(each_word.lower() in each_key):
hit_cnt = hit_cnt + 1
key_and_hit_cnt_dict[each_key] = hit_cnt
hit_cnt=0
sorted_keys_wrt_hitcnt = sorted(key_and_hit_cnt_dict, key= key_and_hit_cnt_dict.__getitem__,reverse=True)
i=0
for each_entry in sorted_keys_wrt_hitcnt:
if(i<5):
location=news_index[each_entry]
item.append({ 'body' : data[location]["body"],'location':location,'key':each_entry,'words':input_headline_list})
i = i+1
return jsonify({'item':item})
编辑
这是我的日志
2014-03-19T10:09:08.000898+00:00 app[web.1]: 2014-03-19 10:09:07 [7] [INFO] Booting worker with pid: 7
2014-03-19T10:09:08.262376+00:00 heroku[web.1]: State changed from starting to up
2014-03-19T10:09:18.149027+00:00 heroku[router]: at=info method=GET path=/ host=glacial- plateau-3427.herokuapp.com request_id=203107b5-5c0e-40bd-8e0b-4cdc8649c2f1 fwd="27.251.95.162" dyno=web.1 connect=2ms service=25ms status=404 bytes=384
2014-03-19T10:09:24.995531+00:00 heroku[router]: at=info method=GET path=/toi/cricket host=glacial-plateau-3427.herokuapp.com request_id=18d612f6-7cf6-4fc0-a686-6a8680cf469f fwd="27.251.95.162" dyno=web.1 connect=2ms service=18ms status=500 bytes=454
2014-03-19T10:10:45.866027+00:00 heroku[router]: at=info method=GET path=/toi/cricket%20India%20T20 host=glacial-plateau-3427.herokuapp.com request_id=5122179a-dfde-4a22-b916-daa7eec3ec10 fwd="27.251.95.162" dyno=web.1 connect=1ms service=6ms status=500 bytes=454
2014-03-19T10:13:39.713629+00:00 heroku[router]: at=info method=GET path=/toi/aap%20modi%20kejriwal host=glacial-plateau-3427.herokuapp.com request_id=0426e03c-61bd-4b4f-995b-55a72c91d676 fwd="27.251.95.162" dyno=web.1 connect=1ms service=5ms status=500 bytes=454
1 个回答
1
正如我们在聊天中讨论的那样:
- 你在代码中访问的所有文件都在git里。
- 你并没有从代码中修改任何文件。
- 运行代码时出现500错误。
我觉得你应该检查一下你的代码,可能是生成了一些现在没有处理的异常。你可以把你的代码放在一个try-except块里,然后打印出异常信息。比如:
try:
news_index=shelve.open('IndexedMapping')
item = [] # list for storing the final results
with open('TodaysToiScrapedItemsOutput.json') as f: #load json file
data = json.load(f)
input_headline = news_string
input_headline_list = input_headline.split()
temp_input_headline_list = []
for each_word in input_headline_list:
temp_input_headline_list.append(each_word)
for each_word in temp_input_headline_list:
if (each_word.lower() in ignore_this_words):
input_headline_list.remove(each_word)
hit_cnt=0
key_and_hit_cnt_dict={}
for each_key in news_index:
for each_word in input_headline_list:
if(each_word.lower() in each_key):
hit_cnt = hit_cnt + 1
key_and_hit_cnt_dict[each_key] = hit_cnt
hit_cnt=0
sorted_keys_wrt_hitcnt = sorted(key_and_hit_cnt_dict, key= key_and_hit_cnt_dict.__getitem__,reverse=True)
i=0
for each_entry in sorted_keys_wrt_hitcnt:
if(i<5):
location=news_index[each_entry]
item.append({ 'body' : data[location]["body"],'location':location,'key':each_entry,'words':input_headline_list})
i = i+1
return jsonify({'item':item})
except Exception, e:
import traceback
traceback.print_exc()
return jsonify({'error': str(e)})