简体   繁体   English

Python套接字未发送主机名

[英]Python socket not sending host name

I am using an asyncore.dispatcher client on python to connect to a server developed in LabWindows running on a PC. 我在python上使用asyncore.dispatcher客户端连接到在PC上运行的LabWindows中开发的服务器。 Here's the code snippet on the client that connects to the server: 这是连接到服务器的客户端上的代码片段:

class DETClient(asyncore.dispatcher):

   def __init__(self, host, port):
      asyncore.dispatcher.__init__(self)
      self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
      self.connect((host,port))

On the server side, my Labwindows code is looking for two parameters, TCPPeerName and TCPPeerAddr: 在服务器端,我的Labwindows代码正在寻找两个参数TCPPeerName和TCPPeerAddr:

GetTCPPeerName (handle, peerName, sizeof (peerName));
GetTCPPeerAddr (handle, peerAddress, sizeof (peerAddress));

It seems that the python code is not passing the hostname at all, because my server gets a NULL for PeerName. 似乎python代码根本没有传递主机名,因为我的服务器为PeerName获取了NULL。

Do I have to do anything to specifically make the asyncore client to send the PeerName when establishing a connection? 建立连接时,我是否需要做任何事情来专门使异步客户端发送PeerName?

Do I have to do anything to specifically make the asyncore client to send the PeerName when establishing a connection? 建立连接时,我是否需要做任何事情来专门使异步客户端发送PeerName?

No, you don't, because TCP clients don't send names when establishing a connection. 不,您不需要,因为建立连接时TCP客户端不会发送名称。 They send addresses. 他们发送地址。

GetTCPPeerName is almost certainly calling gethostbyaddr(X) , where X is the address returned by GetTCPPeerAddr . 几乎可以肯定, GetTCPPeerName会调用gethostbyaddr(X) ,其中XGetTCPPeerAddr返回的地址。 In your case gethostbyaddr() is failing because the information is not available. 在您的情况下, gethostbyaddr()失败,因为该信息不可用。

This means that your hostname resolution database is missing some data -- you might need to update your DNS, your hosts file, your WINS data, or wherever your host name data lives. 这意味着您的主机名解析数据库缺少某些数据-您可能需要更新DNS,主机文件,WINS数据或主机名数据所在的任何位置。

IS that DETClient the actual implementation? 那是DETClient的实际实现吗? Because it's missing instantiations of both host and port for that instance? 因为缺少该实例的主机和端口的实例化? in self.connect you're using self.host and self.port, which both evaulate to None since you havent set them yet. 在self.connect中,您使用的是self.host和self.port,由于您尚未设置它们,所以两者都都撤回了None。

class DETClient(asyncore.dispatcher):

  def __init__(self, host, port):
    self.host = host #<---- this is missing
    self.port = port #<---- this is missing
    asyncore.dispatcher.__init__(self)
    self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
    self.connect((self.host,self.port))

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

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