2017-07-10 30 views
1

有以下服务器代码(我定义从插座继承的类,我试图使用类调用客户端插座上的发送方法):类型错误不断变化的Socket对象的__class__属性

import socket 

class LogSocket(socket.socket): 
    def send(self, data): 
     print("Sending {0} to {1}".format(
      data, self.getpeername()[0])) 
     super().send(data) 

def respond(client): 
    response = input("Enter a value: ") 
    client.send(bytes(response, 'utf8')) 
    client.close() 

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
server.bind(('',2401)) 
server.listen(1) 
try: 
    while True: 
     client, addr = server.accept() 
     client.__class__ = LogSocket 
     respond(client) 
finally: 
    server.close() 

客户端代码是:

import socket 

client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
client.connect(('localhost', 2401)) 
print("Received: {0}".format(client.recv(1024))) 
client.close() 

当我运行上面的代码中,服务器崩溃,并显示错误:

Traceback (most recent call last): 
    File "10_04_server.py", line 20, in <module> 
    client.__class__ = LogSocket 
TypeError: __class__ assignment: 'LogSocket' object layout differs from 'socket' 

这个错误是什么意思?为什么会发生?

回答

1

设置__slots__属性(),而这将工作:

class LogSocket(socket.socket): 
    __slots__ =() 
    def send(self, data): 
     print("Sending {0} to {1}".format(
      data, self.getpeername()[0])) 
     super().send(data) 

relevant pieces of documentation是:

The action of a __slots__ declaration is limited to the class where it is defined. As a result, subclasses will have a __dict__ unless they also define __slots__ (which must only contain names of any additional slots).

而且

__class__ assignment works only if both classes have the same __slots__ .

+0

由于它的工作。该错误消息不是很有帮助。 – debashish

+0

当我们分配一个空值时,套接字类有一个非空的'__slots__'值。如果这些类有不同的'__slots__',那么'__class__'赋值如何工作? – debashish

+0

再次阅读第一个文档片段。子类中的__slots__是_additional_ slots。 – Eric

相关问题