在我的 python 代码中,我无法连接到 Openfire 服务器。
我想连接到 OpenFire 服务器
我的代码是
import threading
import tkinter as tk
from tkinter import simpledialog, scrolledtext
import json
import logging
import asyncio
from slixmpp import ClientXMPP
class EchoBot(ClientXMPP):
def __init__(self, jid, password):
super().__init__(jid, password)
self.add_event_handler("session_start", self.start)
self.add_event_handler("message", self.message)
async def start(self, event):
self.send_presence()
await self.get_roster()
async def message(self, msg):
if msg['type'] in ('chat', 'normal'):
msg.reply(f"Thanks for sending\n{msg['body']}").send(mto=msg['from'].boundjid.full)
class ChatApp:
CONFIG_FILE = 'config.json'
def __init__(self, root):
self.root = root
self.root.title("Chat Application")
self.chat_client = None
top_frame = tk.Frame(root)
top_frame.pack(side=tk.LEFT, fill=tk.Y)
self.change_server_button = tk.Button(top_frame, text="Other Server?", command=self.open_connect_dialog)
self.change_server_button.pack(side=tk.TOP)
self.search_bar = tk.Entry(top_frame, bd=1)
self.search_bar.pack(padx=10, pady=10, side=tk.TOP)
self.search_bar.bind("<KeyRelease>", self.filter_channels)
self.channel_list = tk.Listbox(top_frame)
self.channel_list.pack(fill=tk.BOTH, expand=True)
self.all_channels = ["#Firma", "General", "Random", "Tech", "BLLLLLAAAAAAA"]
for channel in self.all_channels:
self.channel_list.insert(tk.END, channel)
self.chat_display = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED)
self.chat_display.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
self.message_entry = tk.Entry(root, bd=1, width=100)
self.message_entry.pack(padx=10, pady=10, fill=tk.X, side=tk.LEFT, expand=True)
self.message_entry.bind("<Return>", self.send_message)
self.send_button = tk.Button(root, text="Send", command=self.send_message)
self.send_button.pack(padx=10, pady=10, side=tk.RIGHT)
self.load_config()
def filter_channels(self, event):
search_term = self.search_bar.get().lower()
self.channel_list.delete(0, tk.END)
for channel in self.all_channels:
if search_term in channel.lower():
self.channel_list.insert(tk.END, channel)
def open_connect_dialog(self):
dialog = ConnectDialog(self.root, self.config)
if dialog.result:
server, jid, password, port = dialog.result
self.config = {'server': server, 'jid': jid, 'password': password, 'port': port}
self.save_config()
self.connect_to_server(server, jid, password, port)
def load_config(self):
try:
with open(self.CONFIG_FILE, 'r') as file:
self.config = json.load(file)
except (FileNotFoundError, json.JSONDecodeError):
self.config = {
'server': '',
'jid': '',
'password': '',
'port': ''
}
def save_config(self):
with open(self.CONFIG_FILE, 'w') as file:
json.dump(self.config, file)
def connect_to_server(self, server, jid, password, port):
logging.basicConfig(level=logging.DEBUG, format='%(levelname)-8s %(message)s')
self.chat_client = EchoBot(jid, password)
addresse = tuple
addresse = [server, port]
try:
# Verwende den richtigen Port und Server aus deiner Serverkonfiguration
if self.chat_client.connect(addresse):
# Starte die Schleife mit asyncio.run
asyncio.run(self.chat_client.process(block=False))
self.chat_client.starttls()
self.chat_display.config(state=tk.NORMAL)
self.chat_display.insert(tk.END, "Connected to server.\n")
self.chat_display.config(state=tk.DISABLED)
else:
raise ConnectionError("Unable to connect.")
except Exception as e:
self.chat_display.config(state=tk.NORMAL)
self.chat_display.insert(tk.END, f"Error: {str(e)}\n")
self.chat_display.config(state=tk.DISABLED)
logging.error(f"Error: {str(e)}")
def display_message(self, message):
self.chat_display.config(state=tk.NORMAL)
self.chat_display.insert(tk.END, message)
self.chat_display.config(state=tk.DISABLED)
def send_message(self, event=None):
message = self.message_entry.get()
if message and self.chat_client:
try:
self.chat_client.send_message(mto=self.config['jid'], mbody=message, mtype='chat')
self.display_message(f"Me: {message}\n")
self.message_entry.delete(0, tk.END)
except Exception as e:
self.display_message(f"Send Error: {str(e)}\n")
class ConnectDialog(simpledialog.Dialog):
def __init__(self, parent, config):
self.config = config
super().__init__(parent)
def body(self, master):
self.title("Connection Settings")
tk.Label(master, text="Server Address:").grid(row=0)
tk.Label(master, text="Username (JID):").grid(row=1)
tk.Label(master, text="Password:").grid(row=2)
tk.Label(master, text="Port:").grid(row=3)
self.server_entry = tk.Entry(master)
self.jid_entry = tk.Entry(master)
self.password_entry = tk.Entry(master, show='*')
self.port_entry = tk.Entry(master)
self.server_entry.grid(row=0, column=1)
self.jid_entry.grid(row=1, column=1)
self.password_entry.grid(row=2, column=1)
self.port_entry.grid(row=3, column=1)
self.server_entry.insert(0, self.config['server'])
self.jid_entry.insert(0, self.config['jid'])
self.password_entry.insert(0, self.config['password'])
self.port_entry.insert(0, str(self.config['port']))
return self.server_entry
def apply(self):
server = self.server_entry.get()
jid = self.jid_entry.get()
password = self.password_entry.get()
port = int(self.port_entry.get())
self.result = (server, jid, password, port)
def main():
root = tk.Tk()
chat_app = ChatApp(root)
root.mainloop()
if __name__ == "__main__":
main()
我的调试:
DEBUG Using proactor: IocpProactor
DEBUG Loaded Plugin: RFC 6120: Stream Feature: STARTTLS
DEBUG Loaded Plugin: RFC 6120: Stream Feature: Resource Binding
DEBUG Loaded Plugin: RFC 3920: Stream Feature: Start Session
DEBUG Loaded Plugin: RFC 6121: Stream Feature: Roster Versioning
DEBUG Loaded Plugin: RFC 6121: Stream Feature: Subscription Pre-Approval
DEBUG Loaded Plugin: RFC 6120: Stream Feature: SASL
DEBUG Event triggered: connecting
ERROR Error: Unable to connect.
服务器地址、Jid、密码、端口是否正确
您需要启用连接错误的日志记录。默认情况下很有可能需要 TLS,但您的服务器(本地?)没有启用 TLS。
检查
slixmpp.connect()
来源:
请参阅
force_starttls: bool = True
参数。如果服务器未启用 TLS 并拒绝 START TLS
命令,它将无法连接。
因此尝试将参数指定为 False。
作为侧面推荐检查Gajim 来源,它也是用 Python 编写的。