用Python实现即时通讯与替代Shell
即时通讯客户端开发
在开发即时通讯客户端时,我们需要完成客户端配置、消息处理、网络连接等功能。
客户端配置
客户端配置代码在将控件添加到主窗口后运行,它会向用户询问服务器地址、端口和用户名,并尝试连接到服务器。以下是配置函数的代码:
def configure(self): # Performs the steps to connect to the server # Show a dialog box asking for server address followed by a port server = self.ask_for_info("server_address:port") # Regex that crudely matches an IP address and a port number regex = re.search('^(\d+\.\d+\.\d+\.\d+):(\d+)$', server) address = regex.group(1).strip() port = regex.group(2).strip() # Ask for a username self.username = self.ask_for_info("username") self.username_label.set_text(self.username) # Attempt to connect to the server and then start l