Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过电子邮件使用RPC?_Python_Web Services_Email_Asynchronous - Fatal编程技术网

Python 通过电子邮件使用RPC?

Python 通过电子邮件使用RPC?,python,web-services,email,asynchronous,Python,Web Services,Email,Asynchronous,有人能告诉我他们是如何以某种形式做到这一点的吗,不管是XML-RPC、SOAP、定制等等。不要太担心数据包格式 我感兴趣的是通过电子邮件进行某种RPC,设置一个程序,通过电子邮件接收来自其他应用程序甚至来自订阅列表用户的命令。基本上,这个想法是你给某人一个电子邮件地址,然后向活动命令发送消息,然后回复到电子邮件中。我举一个很好的例子,可能是一个国际象棋程序,在这个程序中,时间是不相关的,但传递就是一切,动作的顺序是给定的 我最感兴趣的是其他人的经历,尤其是电子邮件的性质和我应该注意的任何特殊行为

有人能告诉我他们是如何以某种形式做到这一点的吗,不管是XML-RPC、SOAP、定制等等。不要太担心数据包格式

我感兴趣的是通过电子邮件进行某种RPC,设置一个程序,通过电子邮件接收来自其他应用程序甚至来自订阅列表用户的命令。基本上,这个想法是你给某人一个电子邮件地址,然后向活动命令发送消息,然后回复到电子邮件中。我举一个很好的例子,可能是一个国际象棋程序,在这个程序中,时间是不相关的,但传递就是一切,动作的顺序是给定的

我最感兴趣的是其他人的经历,尤其是电子邮件的性质和我应该注意的任何特殊行为

我对RPC over Message queue和异步传递有相当多的经验,但我想提出一个解决方案,将通信转移到hotmail或gmail上,从而释放我的服务器和异步对讲机的麻烦。

您可以像这样使用

#!/bin/bash
while sleep 1
do
    fetchmail --idle --mda python program_that_accepts_email_with_headers_on_stdin.py
done
#!/usr/bin/env python

import rfc822
import logging

from google.appengine.ext import db
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app

class LogSenderHandler(InboundMailHandler):
    def receive(self, message):
        service_name, service_email = rfc822.parseaddr(message.to)
        service_request = service_email.split('@').pop(0)
        sender_name, sender_email = rfc822.parseaddr(message.sender)

        logging.info('Service `%s` activated by `%s`.' % (service_request, sender_email))

if __name__ == '__main__':
    application = webapp.WSGIApplication(
        [LogSenderHandler.mapping()])
    run_wsgi_app(application)
然后,您的程序可以做各种事情,从查询数据库,通过使用一些http服务,到向某人发送电子邮件。它的工作方式是先休眠一秒钟,然后根据您需要在~/.fetchmailrc中设置的内容检查邮箱(阅读
manfetchmail
,了解如何操作)。如果它找到任何电子邮件,它将调用您的程序,如果没有,循环将返回到起点。如果找不到任何电子邮件,它将等待,直到电子邮件到达(或者邮件服务器将重新启动)

底线是,如果所述系统负载不重,它几乎会立即对电子邮件做出反应(通常你发送一封带有命令的电子邮件,等待3秒钟,收件箱中就会有回复)


注意:停止(
--idle
)仅适用于IMAP服务器。有了pop服务器,你也可以做同样的事情,但是睡眠10秒而不是1秒。睡眠1秒是好的,因为你的程序可能会超快,可能会与其他程序产生无限循环(f.e.mailer daemon说有人在度假),通常最好至少将他们的循环狂热限制在每秒1封电子邮件。我从中学到了这一点:)如果你想每秒处理一封以上的电子邮件,那么睡觉是不好的。如果是这样,请将
sleep 1
切换到
true

我已经在用Google AppEngine和Python做这件事了。这真的很简单

在您的
app.yaml
文件中,您需要设置您将使用电子邮件服务并使用您的文件来管理它们:

application: appname
version: 1
runtime: python
api_version: 1

handlers:
- url: /_ah/mail/.+
  script: mail.py
  login: admin

inbound_services:
- mail
然后使用如下内容创建一个
mail.py
文件:

#!/bin/bash
while sleep 1
do
    fetchmail --idle --mda python program_that_accepts_email_with_headers_on_stdin.py
done
#!/usr/bin/env python

import rfc822
import logging

from google.appengine.ext import db
from google.appengine.ext import webapp 
from google.appengine.ext.webapp.mail_handlers import InboundMailHandler 
from google.appengine.ext.webapp.util import run_wsgi_app

class LogSenderHandler(InboundMailHandler):
    def receive(self, message):
        service_name, service_email = rfc822.parseaddr(message.to)
        service_request = service_email.split('@').pop(0)
        sender_name, sender_email = rfc822.parseaddr(message.sender)

        logging.info('Service `%s` activated by `%s`.' % (service_request, sender_email))

if __name__ == '__main__':
    application = webapp.WSGIApplication(
        [LogSenderHandler.mapping()])
    run_wsgi_app(application)

您只需向
servicename@appname.appspotmail.com
。瞧

+1用于考虑HTTP的替代方案。我真的很想看看是否可以通过Gopher协议完成。:-)@apheta crickey这是过去的一次爆炸,加上1,甚至记住了gopher,并在对话中提到:)我会严格使用电子邮件系统来传递消息,它在运行中是无状态的。我想的是SOAP优于电子邮件,可以说是电子邮件协议,但我想的是Google Mail或hotmail。我想这些都是通过http操作的。很抱歉@amphet。很酷。我喜欢。需要谷歌提供服务吗?很久没有看appengine了,但我喜欢这种方法+1你可以在问题中这么说;)我只使用linux,所以它对我来说非常好。我喜欢fetchmail选项,循环是如何工作的,你能举个例子吗,循环会不会很贵,想知道更多,如果使用bash脚本,我将如何休眠任务?