我遇到此错误:AttributeError:'socket'对象属性'sendto'是只读的
这行代码出错
self.server.socket.sendto =模拟.MagicMock()
我仅在python 3中收到此错误,而在python 2中没有..
有人可以帮助我解决此问题吗,根本原因是什么?
下面是完整的代码
下面是文件test_servers.py
import mock
import unittest2 as unittest
import servers
class TestSVPServer(unittest.TestCase):
@mock.patch("servers.threading")
def setUp(self, *args):
self.address_ = ('localhost', 8080)
self.handler = mock.MagicMock()
self.server = servers.SVPServer(address=self.address_, handler=self.handler)
@mock.patch("servers.time")
def test_sendto(self, *args):
data = "test data"
address = ('127.0.0.1', 8080)
time = 10
servers.time.time.return_value = time
self.server.socket.sendto = mock.MagicMock()
下面是另一个文件server.py文件的内容
import asyncore
import select
import socket
import threading
import time
class AsyncoreUDPServer(asyncore.dispatcher, object):
BUFFER_SIZE = 4096
def __init__(self, address, handler):
asyncore.dispatcher.__init__(self)
self.handler = handler
self.create_socket(socket.AF_INET, socket.SOCK_DGRAM)
self.set_reuse_addr()
self.bind(address)
def handle_read(self):
self.handler(*self.socket.recvfrom(self.BUFFER_SIZE))
def writable(self):
return False
class SVPServer(AsyncoreUDPServer):
def __init__(self, *args, **kwargs):
super(SVPServer, self).__init__(*args, **kwargs)
self.last_message_sent_timestamp = 0
def sendto(self, *args, **kwargs):
self.last_message_sent_timestamp = time.time()
self.socket.sendto(*args, **kwargs)