【问题标题】:Attribute error in a simple xmlrpc python script简单 xmlrpc python 脚本中的属性错误
【发布时间】:2012-09-12 08:08:46
【问题描述】:

我有一个简单的 xmlrpc 服务器设置来启动一个 SMTP 服务器,代码在这里:

from SimpleXMLRPCServer import SimpleXMLRPCServer
import smtplib

# Create server
server = SimpleXMLRPCServer(("localhost", 1025), allow_none = True)

# add the introspection functions (system.listMethods, system.methodHelp 
# and system.methodSignature)
server.register_introspection_functions()

def send(host, port):
    server = smtplib.SMTP((host, port), None)

# register this method
server.register_function(send, 'send')

# start server
server.serve_forever()

我启动此服务器并在客户端执行以下步骤:

import xmlrpclib
s = xmlrpclib.ServerProxy('http://localhost:1025')
s.send('0.0.0.0',25)

导致以下错误我不明白:

xmlrpclib.Fault: <Fault 1: "<type 'exceptions.AttributeError'>:'tuple' object has no attribute 'find'">

这里的元组对象是什么意思?为什么代码需要属性查找?有什么想法可以帮助我使此代码正常工作,即我能够发出 xmlrpc 请求来初始化(并稍后使用)xmlrpc 服务器内的 smtp 服务器?

谢谢 亚历克斯

【问题讨论】:

    标签: python xml-rpc smtplib


    【解决方案1】:

    smtplib documentation 中声明SMTP 类的签名接受两个不同的主机和端口参数。

    因此你应该这样定义你的发送函数:

    def send(host, port):
        server = smtplib.SMTP(host, port)
    

    SMTP 构造函数可能需要一个字符串作为主机,并使用 find 方法。 但是如果你传入元组(host, port),那么AttributeError就会生成。

    【讨论】:

    • 因为我之前在 smtplib.SMTP((host,port),(host1,port1)) 或 smtplib.SMTP((host,port),None) 版本中使用过 smtplib.SMTP有点困惑为什么你的建议有效。但它有效,所以这是一个答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    相关资源
    最近更新 更多