Python Heroku应用失败,并显示“正在使用地址”错误

时间:2019-03-04 13:44:24

标签: python heroku flask

已在Heroku中部署了一个基本的python / flask应用(以下代码)。据我所知,procfile是正确的,并且要求是最新的。 该应用程序基本上通过浏览器接收两个参数,并返回JSON。我对heroku和procfile比较陌生,所以我担心这很简单。

但是,一旦尝试在浏览器中打开链接,我的日志中就会出现此错误:

  

OSError:[Errno 98]地址已在使用中

我的procfile正确吗?

请找到以下代码以供参考:

Procfile:

web: gunicorn SA2:SAapp

Python代码:

from textblob import TextBlob
import tweepy
import matplotlib.pyplot as plt
from flask import Flask
import json

SAapp = Flask("Sentwinel")

#Function that collects the percentages
def percentage(part, whole):
    return 100 * float(part)/float(whole)

#Setting variables for the auth information that allows us to access twitters API
consumerKey = "3DOvT3TjEgd16Yk7xvNxNjUMQ"
consumerSecret = "DeMxglGqNdO9A1xwE8PfI4IMTPFnL6jAihxunsA45zlxfwW9bk"
accessToken = "381544613-Zda1F8KbIZ0q1Eyz1azIpllKu9eimHaUkJNZpioa"
accessTokenSecret = "GwtenTAoU3Bki2F1MvnbNRxm3XahX0O8vRx8eFqC8SVoR"

#Connects to the twitter API
auth = tweepy.OAuthHandler(consumerKey, consumerSecret)
auth.set_access_token(accessToken, accessTokenSecret)
api = tweepy.API(auth)

#The function that gets the search terms and returns the SA
@SAapp.route("/<searchTerm>/<noOfSearchTerms>")
def GetPlot (searchTerm, noOfSearchTerms):
    noOfSearchTerms = int(noOfSearchTerms)
    tweets = tweepy.Cursor(api.search, q=searchTerm).items(noOfSearchTerms)

    positive = 0
    negative = 0
    neutral = 0
    polarity = 0

    for tweet in tweets:
        print(tweet.text)
        analysis = TextBlob(tweet.text)
        polarity += analysis.sentiment.polarity

        if (analysis.sentiment.polarity == 0):
            neutral += 1
        elif (analysis.sentiment.polarity < 0.00):
            negative += 1
        elif (analysis.sentiment.polarity > 0.00):
            positive += 1

    #Shows the percentages of how many tweets per emotion per how many search terms.
    positive = percentage(positive, noOfSearchTerms)
    negative = percentage(negative, noOfSearchTerms)
    neutral = percentage(neutral, noOfSearchTerms)
    polarity = percentage(polarity, noOfSearchTerms)

    #Formats the polarity to show to 2 decimal places
    #positive = format(positive, '.2f')
    #negative = format(negative, '.2f')
    #neutral = format(neutral, '.2f')

    print("How people are reacting on " + searchTerm + "by analyzing" + str(noOfSearchTerms) + " Tweets.")
    if (polarity == 0 ):
        print("Neutral")
    elif (polarity < 0.00 ):
        print("Negative")
    elif (polarity > 0.00 ):
        print("Postitive")

    #Returns the polarity scores as JSON integers, for Android Studio
    return json.dumps({"positive":positive,"negative":negative,"neutral":neutral})


SAapp.run()

2 个答案:

答案 0 :(得分:1)

经过深夜研究,我终于找到了解决方案。感谢this这样的问题。

问题不在procfile中,而是在python / flask代码中。我需要向烧瓶应用程序添加封装。

我更改了这一行:

SAapp = Flask("Sentwinel")

对此:

SAapp = Flask(__name__)

并添加以下行:

if __name__ == "main":

在我的SAapp.run()之上。

想补充,以防有人遇到相同问题

答案 1 :(得分:1)

添加if __name__ == "__main__"

SAapp.run()之前