简体   繁体   English

简单的xmlrpc python脚本中的属性错误

[英]Attribute error in a simple xmlrpc python script

I have a simple xmlrpc server setup to start a SMTP server, the code is here: 我有一个简单的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()

I start this server and on the client side I perform the following steps: 我启动此服务器,并在客户端执行以下步骤:

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

which result in the following error I do not understand: 这导致以下我不明白的错误:

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

What tuple object is meant here? 元组对象在这里是什么意思? Why does the code require an attribute find? 为什么代码要求找到属性? Any ideas that help me to get this code working, ie that I am able to make a xmlrpc request to initialize (and later use) a smtp server inside the xmlrpc server? 有什么想法可以帮助我使此代码正常工作,即,我能够发出xmlrpc请求以初始化(并在以后使用)xmlrpc服务器内部的smtp服务器吗?

Thanks Alex 谢谢亚历克斯

In the smtplib documentation it's stated the the signature of the SMTP class accepts two distinct parameters for host and port. smtplib文档中 ,说明了SMTP类的签名接受host和port的两个不同参数。

Thus you should define your send function in this way: 因此,您应该以这种方式定义send函数:

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

Probably the SMTP constructor expects a string as host, and uses the find method. SMTP构造函数可能希望将字符串作为主机,并使用find方法。 But if you pass in the tuple (host, port) then that AttributeError is generated. 但是,如果您传入元组(host, port) ,则会生成AttributeError

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

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