简体   繁体   English

C# 中是否有等效于.isConnected 功能的 python

[英]Is there a python equivalent for .isConnected functionality in C#

I have been looking for the equivalent of [isConnected() functionality] of C# in PYTHON.我一直在 PYTHON 中寻找与 C# 等效的 [isConnected() 功能]。 https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.connected?view=net-5.0 https://docs.microsoft.com/en-us/dotnet/api/system.net.sockets.socket.connected?view=net-5.0

What I basically need is to check if the socket connection is still open on another end (primary server).我基本上需要的是检查套接字连接在另一端(主服务器)是否仍然打开。 If not, move the connection to a new socket(backup server).如果没有,请将连接移动到新的套接字(备份服务器)。

I have been quite blank as to how I can move the existing connection to another server without having to reconnect again.对于如何将现有连接移动到另一台服务器而无需重新连接,我一直很空白。 Any kind of guidance and help will be really appreciated.任何形式的指导和帮助将不胜感激。

Right now, my connection to server is done at Login().现在,我与服务器的连接是在 Login() 处完成的。 I want that just in case primary disconnects, the existing username moves to secondary.我希望以防万一主要断开连接,现有用户名移动到次要用户名。 He just should perform file and word operations.他只应该执行文件和文字操作。 What changes should I do in my code to achieve this.我应该在我的代码中做哪些改变来实现这一点。

My current code structure is: Client side:我当前的代码结构是:客户端:

 def file_operation():
      do_something

 def word_addition():
      do_something

 def login():
      s.connect(host,port)

if __name__ == "__main__":

Server side:服务器端:

  def accept_client():
      s.accept
      do_something

  def GUI():
     accept_client()

if __name__ == "__main__":

According to the Python Sockets documentation , no, there isn't.根据Python Sockets 文档,不,没有。 Python sockets are merely a wrapper around the Berkeley Sockets API, so whatever is in that API, that's what's in the Python API. Python sockets are merely a wrapper around the Berkeley Sockets API, so whatever is in that API, that's what's in the Python API.

If you look at the source code for Microsoft's Socket.cs class , you'll see that they maintain a boolean flag field m_IsConnected to indicate the socket's current connection status.如果您查看Microsoft 的 Socket.cs class 的源代码,您会看到它们维护一个boolean 标志字段m_IsConnected以指示套接字的当前连接状态。 You could potentially do the same with your own custom sockets class, using Microsoft's code as a model for writing your Python code.您可以对自己的自定义 sockets class 执行相同的操作,使用 Microsoft 的代码作为 model 来编写您的 ZA7F17F35426B938241 代码。

Or, you could simply use the socket as you normally would, and switch to a different server when a socket error occurs.或者,您可以像往常一样简单地使用套接字,并在发生套接字错误时切换到不同的服务器。

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

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