Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_ams_packet(self):
source_addr = AmsAddr("127.0.0.1.1.1", 59558)
target_addr = AmsAddr("172.38.1.1.1.1", 59559)
header1 = structs.AmsHeader(
target_net_id=target_addr.netIdStruct(),
target_port=target_addr.port,
source_net_id=source_addr.netIdStruct(),
source_port=source_addr.port,
command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
data_length=6,
error_code=1,
invoke_id=2,
)
ads_data = b"\x00" * 6
amstcpheader = structs.AmsTcpHeader(header1.length + len(ads_data))
packet = structs.AmsPacket(amstcpheader, header1, ads_data)
def test_amsheader(self):
source_addr = AmsAddr("127.0.0.1.1.1", 59558)
target_addr = AmsAddr("172.38.1.1.1.1", 59559)
header1 = structs.AmsHeader(
target_net_id=target_addr.netIdStruct(),
target_port=target_addr.port,
source_net_id=source_addr.netIdStruct(),
source_port=source_addr.port,
command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
data_length=6,
error_code=1,
invoke_id=2,
)
data_bytes = header1.to_bytes()
self.assertEqual(len(data_bytes), header1.length)
def test_ams_packet(self):
source_addr = AmsAddr("127.0.0.1.1.1", 59558)
target_addr = AmsAddr("172.38.1.1.1.1", 59559)
header1 = structs.AmsHeader(
target_net_id=target_addr.netIdStruct(),
target_port=target_addr.port,
source_net_id=source_addr.netIdStruct(),
source_port=source_addr.port,
command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
data_length=6,
error_code=1,
invoke_id=2,
)
ads_data = b"\x00" * 6
amstcpheader = structs.AmsTcpHeader(header1.length + len(ads_data))
packet = structs.AmsPacket(amstcpheader, header1, ads_data)
def __init__(self, handler=None, ip_address='', ams_net_id=AMS_NET_ID,
port=ADS_PORT, logging=True):
self.handler = handler or AdvancedHandler()
self.ip_address = ip_address
self.ams_addr = AmsAddr(ams_net_id, port)
self.port = port
self._run = False
self.clients = []
self.request_history = []
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.bind((self.ip_address, self.port))
atexit.register(self.close)
super(Testserver, self).__init__()
self.daemon = True
def test_amsheader(self):
source_addr = AmsAddr("127.0.0.1.1.1", 59558)
target_addr = AmsAddr("172.38.1.1.1.1", 59559)
header1 = structs.AmsHeader(
target_net_id=target_addr.netIdStruct(),
target_port=target_addr.port,
source_net_id=source_addr.netIdStruct(),
source_port=source_addr.port,
command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
data_length=6,
error_code=1,
invoke_id=2,
)
data_bytes = header1.to_bytes()
self.assertEqual(len(data_bytes), header1.length)
header2 = structs.AmsHeader.from_bytes(data_bytes)
def setUpClass(cls):
# Start dummy ADS Endpoint
cls.test_server = AdsTestServer(AdvancedHandler(), logging=False)
cls.test_server.start()
# Endpoint AMS Address
cls.endpoint = AmsAddr(TEST_SERVER_AMS_NET_ID, TEST_SERVER_AMS_PORT)
# Open AMS Port
ads.open_port()
# wait a bit otherwise error might occur
time.sleep(1)
# NOTE: On a Windows machine, this route needs to be configured
# within the router service for the tests to work.
if platform_is_linux():
ads.add_route(cls.endpoint, TEST_SERVER_IP_ADDRESS)
def __init__(self, ams_net_id, ams_net_port, ip_address=None):
# type: (str, int, str) -> None
self._port = None # type: Optional[int]
self._adr = AmsAddr(ams_net_id, ams_net_port)
if ip_address is None:
self.ip_address = ".".join(ams_net_id.split(".")[:4])
else:
self.ip_address = ip_address
self._open = False
self._notifications = {} # type: Dict[int, str]
def adsGetLocalAddressEx(port):
# type: (int) -> AmsAddr
"""Return the local AMS-address and the port number.
:rtype: pyads.structs.AmsAddr
:return: AMS-address
"""
get_local_address_ex = _adsDLL.AdsGetLocalAddressEx
ams_address_struct = SAmsAddr()
error_code = get_local_address_ex(port, ctypes.pointer(ams_address_struct))
if error_code:
raise ADSError(error_code)
local_ams_address = AmsAddr()
local_ams_address._ams_addr = ams_address_struct
return local_ams_address
def add_route(adr, ip_address):
# type: (Union[str, AmsAddr], str) -> None
"""Establish a new route in the AMS Router (linux Only).
:param adr: AMS Address of routing endpoint as str or AmsAddr object
:param str ip_address: ip address of the routing endpoint
"""
if isinstance(adr, str):
adr = AmsAddr(adr)
return adsAddRoute(adr.netIdStruct(), ip_address)
def adsGetLocalAddress():
# type: () -> AmsAddr
"""Return the local AMS-address and the port number.
:rtype: pyads.structs.AmsAddr
:return: AMS-address
"""
adsGetLocalAddressFct = _adsDLL.AdsGetLocalAddress
stAmsAddr = SAmsAddr()
errCode = adsGetLocalAddressFct(pointer(stAmsAddr))
if errCode:
raise ADSError(errCode)
adsLocalAddr = AmsAddr()
adsLocalAddr._ams_addr = stAmsAddr
return adsLocalAddr