Example #1
0
 def test_tcp_server_close(self):
     """ test that the synchronous TCP server closes correctly """
     with patch.object(socket.socket, 'bind') as mock_socket:
         identity = ModbusDeviceIdentification(info={0x00: 'VendorName'})
         server = ModbusTcpServer(context=None, identity=identity)
         server.threads.append(Mock(**{'running': True}))
         server.server_close()
         self.assertEqual(server.control.Identity.VendorName, 'VendorName')
         self.assertFalse(server.threads[0].running)
Example #2
0
 def test_tcp_server_close(self):
     """ test that the synchronous TCP server closes correctly """
     with patch.object(socket.socket, 'bind') as mock_socket:
         identity = ModbusDeviceIdentification(info={0x00: 'VendorName'})
         server = ModbusTcpServer(context=None, identity=identity)
         server.threads.append(Mock(**{'running': True}))
         server.server_close()
         self.assertEqual(server.control.Identity.VendorName, 'VendorName')
         self.assertFalse(server.threads[0].running)
Example #3
0
 def run(self):
     """
     Start the server.
     """
     while not self.do_stop.is_set():
         try:
             self.srv = ModbusTcpServer(self.context, self.framer,
                                        self.identity, (self.ip, self.port))
             self.srv.allow_reuse_address = True
             self.srv.serve_forever()
         except Exception:
             raise
     print("[*] Server stopping.")
Example #4
0
class Server(threading.Thread):
    """
    Modbus Server class. Holds a datablock and identity. Serves forever (blocks calling thread).
    """
    def __init__(self, datablock, identity):
        threading.Thread.__init__(self)
        self.do_stop = threading.Event()

        self.ip = None
        self.port = None
        self.id = None

        self.srv = None
        self.context = None
        self.datablock = datablock

        self.framer = ModbusSocketFramer
        self.context = ModbusServerContext(slaves=self.datablock.store,
                                           single=True)

        self.identity = ModbusDeviceIdentification()
        self.identity.VendorName = identity["vendorname"]
        self.identity.ProductCode = identity["productcode"]
        self.identity.VendorUrl = identity["vendorurl"]
        self.identity.ProductName = identity["productname"]
        self.identity.ModelName = identity["modelname"]
        self.identity.MajorMinorRevision = '0.3'
        #self.identity.Filter = ''

    def run(self):
        """
        Start the server.
        """
        while not self.do_stop.is_set():
            try:
                self.srv = ModbusTcpServer(self.context, self.framer,
                                           self.identity, (self.ip, self.port))
                self.srv.allow_reuse_address = True
                self.srv.serve_forever()
            except Exception:
                raise
        print("[*] Server stopping.")

    def verify_request(self, request, client_address):
        print('verify_request(%s, %s)', request, client_address)
        log.warning('     SERVER NEW REQUEST, {}'.format(datetime.now()))
        return socketserver.ThreadingTCPServer.verify_request(
            self, request, client_address)

    def stop(self):
        """
        Stop the server.
        """
        self.do_stop.set()
        self.srv.server_close()
        self.srv.shutdown()
        print("[*] Stopping server.")
Example #5
0
 def test_tcp_server_process(self):
     """ test that the synchronous TCP server processes requests """
     with patch('SocketServer.ThreadingTCPServer') as mock_server:
         server = ModbusTcpServer(None)
         server.process_request('request', 'client')
         self.assertTrue(mock_server.process_request.called)
Example #6
0
 def test_tcp_server_process(self):
     """ test that the synchronous TCP server processes requests """
     with patch('SocketServer.ThreadingTCPServer') as mock_server:
         server = ModbusTcpServer(None)
         server.process_request('request', 'client')
         self.assertTrue(mock_server.process_request.called)