简体   繁体   English

后台处理和烧瓶

[英]Background-process and flask

So, I'm pretty new at webapp development and I'm currently building a simple blog engine for learning purposes using python/flask. 因此,我在webapp开发方面还很陌生,目前正在使用python / flask构建一个简单的博客引擎以供学习。

I don't have much problems so far except for a new feature I would like to implement: post a new entry by email. 到目前为止,除了我要实现的新功能之外,我没有太多问题:通过电子邮件发布新条目。

To implement this, I figure I need to have a background process checking multiple emails accounts for new posts. 为了实现这一点,我认为我需要有一个后台过程来检查多个电子邮件帐户中是否有新帖子。 When a new email is found, the process would go and create the new post to be displayed by the fornt-end. 当找到新电子邮件时,该过程将继续并创建要由前端显示的新帖子。

I hope this is correct and if so, I can I go about implementing this? 我希望这是正确的,如果是这样,我可以继续实施吗?

Might be relevant but I'm using sqlalchemy. 可能是相关的,但是我正在使用sqlalchemy。

Thanks 谢谢

I would use one of the incoming mail APIs for this instead of trying to hack something together. 我将为此使用传入的邮件API之一,而不是尝试将某些东西一起砍掉。 For example, the Sendgrid API will accept an email and then POST some JSON to your app. 例如,Sendgrid API将接受电子邮件,然后将一些JSON发布到您的应用程序。

With this fake Post class: 使用这个伪造的Post类:

class Post(db.Model):
    text = TextProperty()
    title = TextProperty()

You could have a view like this: 您可能会看到这样的视图:

@app.route('/hook/new-email/', methods=['POST'])
def new_email():
    envelope = json.loads(request.form.get('envelope'))
    to_address = envelope['to'][0]
    from_address = envelope['from']
    text = request.form.get('text')
    subject = request.form.get('subject')

    if from_address == settings.ACCEPTED_AUTHOR:
        post = Post(text=text, title=subject)
        post.put()

Remember that if you allow posts to be authored by simple email, you are going to have to do some sort of checking to make sure that you are not posting each and every spam email you get. 请记住,如果您允许通过简单的电子邮件撰写帖子,则必须进行某种检查以确保您不会发布收到的每一封垃圾邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM