我有由python和http://xmpppy.sourceforge.net/编写的XMPP bot的简单代码
#!/usr/bin/python
# -*- coding: utf-8 -*-
import xmpp
import urllib2
import ConfigParser
config = ConfigParser.ConfigParser()
config.read('ipbot.conf')
##########################
user= (config.get('account', 'login'))
password=(config.get('account', 'password'))
presence=(config.get('presence','presence'))
##########################
jid=xmpp.protocol.JID(user)
client=xmpp.Client(jid.getDomain())
client.connect()
client.auth(jid.getNode(),password)
################Parse IP##################
strURL='http://api.wipmania.com/'
f = urllib2.urlopen(urllib2.Request(strURL))
response = f.read()
ipget= response.split("<br>")
f.close()
#############################################
def status(xstatus):
status=xmpp.Presence(status=xstatus,show=presence,priority='1')
client.send(msging)
def message(conn,mess):
global client
if ( mess.getBody() == "ip" ):
client.send(xmpp.protocol.Message(mess.getFrom(),ipget[1]+" => "+ipget[0]))#Send IP
client.RegisterHandler('message',message)
client.sendInitPresence()
while True:
client.Process(1)
请告诉我,如何将此代码转换为使用http://wokkel.ik.nu/和twistedmatrix.com/ 谢谢。
下面的代码应该可以做到这一点。一些注意事项:
XMPPClient
是建立连接的所谓流管理器, 并负责服务器的身份验证。它与 连接子目录处理程序以处理与XML流it的通信 管理。如果连接丢失,它会自动重新连接。ip
的每个传入消息完成。status
从未被调用。我现在使用PresenceProtocol
每次发送状态的子目录处理程序 已建立连接并进行身份验证。该示例是一个所谓的Twisted应用程序,如docstring中所述,将使用
twistd
启动。这将对进程进行守护,并将日志转到twisted.log
。如果指定-n
(在-y
之前),它将不会分离并登录到控制台。相关问题 更多 >
编程相关推荐