<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Starting a fPolicy server using python in ONTAP Discussions</title>
    <link>https://community.netapp.com/t5/ONTAP-Discussions/Starting-a-fPolicy-server-using-python/m-p/462228#M45083</link>
    <description>&lt;P&gt;&lt;EM&gt;I want to set up a fpolicy server, which I can connect to from ontap.&lt;BR /&gt;&lt;/EM&gt;I have tried setting up one using python as shown below. But I am getting below error when ontap tries to connect to it.&lt;BR /&gt;Error -&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&lt;SPAN class=""&gt;Reason for FPolicy Server Disconnection: FPolicy server sent message in which didn't had correct length format marker.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;FPolicy Server using Python :-&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import socket
import struct
import xml.etree.ElementTree as ET

def start_fpolicy_server(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(5)
    print(f"FPolicy listen-only server started on {host}:{port}")

    while True:
        client_socket, addr = server_socket.accept()
        print(f"Connection from {addr}")
        handle_client(client_socket)

def handle_client(client_socket):
    try:
        while True:
            data = client_socket.recv(4096)
            if not data:
                break
            print(f"Received data: {data}")
            # Process the handshake request
            response = process_handshake_request(data)
            if response:
                print(f"Sending response: {response}")
                client_socket.sendall(response)
    finally:
        client_socket.close()

def process_handshake_request(data):
    try:
        # Print the raw data for debugging
        print("Raw data:", data)

        # Split the data to separate the header and the body
        parts = data.split(b'\n\n', 1)
        if len(parts) != 2:
            raise ValueError("Invalid data format: missing header or body")

        header, body = parts

        # Print the header and body for debugging
        print("Header:", header)
        print("Body:", body)

        # Decode the XML body and remove the null character
        body_str = body.decode('utf-8').rstrip('\x00')

        # Print the decoded body for debugging
        print("Decoded Body:", body_str)

        # Parse the XML body
        root = ET.fromstring(body_str)

        if root.tag == 'Handshake':
            session_id = root.find('SessionId').text
            vs_uuid = root.find('VsUUID').text
            policy_name = root.find('PolicyName').text

            # Construct the handshake response
                                                                                                                                                                                                                            
                   response_body = f'''&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;Header&amp;gt;
    &amp;lt;NotfType&amp;gt;NEGO_RESP&amp;lt;/NotfType&amp;gt;
    &amp;lt;ContentLen&amp;gt;0&amp;lt;/ContentLen&amp;gt;
    &amp;lt;DataFormat&amp;gt;XML&amp;lt;/DataFormat&amp;gt;
&amp;lt;/Header&amp;gt;

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;HandshakeResp&amp;gt;
    &amp;lt;SessionId&amp;gt;{session_id}&amp;lt;/SessionId&amp;gt;
    &amp;lt;VsUUID&amp;gt;{vs_uuid}&amp;lt;/VsUUID&amp;gt;
    &amp;lt;PolicyName&amp;gt;{policy_name}&amp;lt;/PolicyName&amp;gt;
    &amp;lt;ProtVersion&amp;gt;3.0&amp;lt;/ProtVersion&amp;gt;
&amp;lt;/HandshakeResp&amp;gt;'''

            # Encode the response body
            response_body_bytes = response_body.encode('utf-8')

            # Calculate the length of the response body
            response_length = len(response_body_bytes) #+ 4  # Include the length of the length prefix itself

            # Construct the length prefix (4 bytes, network byte order)
            length_prefix = struct.pack('!I', response_length)

            # Combine the length prefix and the response body
            response = length_prefix + response_body_bytes

            return response
    except Exception as e:
        print(f"Error processing handshake request: {e}")
    return None

if __name__ == "__main__":
    start_fpolicy_server("0.0.0.0", 9000)&lt;/LI-CODE&gt;</description>
    <pubDate>Wed, 23 Jul 2025 13:25:46 GMT</pubDate>
    <dc:creator>Aman-Chaurasia</dc:creator>
    <dc:date>2025-07-23T13:25:46Z</dc:date>
    <item>
      <title>Starting a fPolicy server using python</title>
      <link>https://community.netapp.com/t5/ONTAP-Discussions/Starting-a-fPolicy-server-using-python/m-p/462228#M45083</link>
      <description>&lt;P&gt;&lt;EM&gt;I want to set up a fpolicy server, which I can connect to from ontap.&lt;BR /&gt;&lt;/EM&gt;I have tried setting up one using python as shown below. But I am getting below error when ontap tries to connect to it.&lt;BR /&gt;Error -&amp;nbsp;&lt;/P&gt;&lt;P class=""&gt;&lt;SPAN class=""&gt;Reason for FPolicy Server Disconnection: FPolicy server sent message in which didn't had correct length format marker.&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;FPolicy Server using Python :-&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import socket
import struct
import xml.etree.ElementTree as ET

def start_fpolicy_server(host, port):
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server_socket.bind((host, port))
    server_socket.listen(5)
    print(f"FPolicy listen-only server started on {host}:{port}")

    while True:
        client_socket, addr = server_socket.accept()
        print(f"Connection from {addr}")
        handle_client(client_socket)

def handle_client(client_socket):
    try:
        while True:
            data = client_socket.recv(4096)
            if not data:
                break
            print(f"Received data: {data}")
            # Process the handshake request
            response = process_handshake_request(data)
            if response:
                print(f"Sending response: {response}")
                client_socket.sendall(response)
    finally:
        client_socket.close()

def process_handshake_request(data):
    try:
        # Print the raw data for debugging
        print("Raw data:", data)

        # Split the data to separate the header and the body
        parts = data.split(b'\n\n', 1)
        if len(parts) != 2:
            raise ValueError("Invalid data format: missing header or body")

        header, body = parts

        # Print the header and body for debugging
        print("Header:", header)
        print("Body:", body)

        # Decode the XML body and remove the null character
        body_str = body.decode('utf-8').rstrip('\x00')

        # Print the decoded body for debugging
        print("Decoded Body:", body_str)

        # Parse the XML body
        root = ET.fromstring(body_str)

        if root.tag == 'Handshake':
            session_id = root.find('SessionId').text
            vs_uuid = root.find('VsUUID').text
            policy_name = root.find('PolicyName').text

            # Construct the handshake response
                                                                                                                                                                                                                            
                   response_body = f'''&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;Header&amp;gt;
    &amp;lt;NotfType&amp;gt;NEGO_RESP&amp;lt;/NotfType&amp;gt;
    &amp;lt;ContentLen&amp;gt;0&amp;lt;/ContentLen&amp;gt;
    &amp;lt;DataFormat&amp;gt;XML&amp;lt;/DataFormat&amp;gt;
&amp;lt;/Header&amp;gt;

&amp;lt;?xml version="1.0"?&amp;gt;
&amp;lt;HandshakeResp&amp;gt;
    &amp;lt;SessionId&amp;gt;{session_id}&amp;lt;/SessionId&amp;gt;
    &amp;lt;VsUUID&amp;gt;{vs_uuid}&amp;lt;/VsUUID&amp;gt;
    &amp;lt;PolicyName&amp;gt;{policy_name}&amp;lt;/PolicyName&amp;gt;
    &amp;lt;ProtVersion&amp;gt;3.0&amp;lt;/ProtVersion&amp;gt;
&amp;lt;/HandshakeResp&amp;gt;'''

            # Encode the response body
            response_body_bytes = response_body.encode('utf-8')

            # Calculate the length of the response body
            response_length = len(response_body_bytes) #+ 4  # Include the length of the length prefix itself

            # Construct the length prefix (4 bytes, network byte order)
            length_prefix = struct.pack('!I', response_length)

            # Combine the length prefix and the response body
            response = length_prefix + response_body_bytes

            return response
    except Exception as e:
        print(f"Error processing handshake request: {e}")
    return None

if __name__ == "__main__":
    start_fpolicy_server("0.0.0.0", 9000)&lt;/LI-CODE&gt;</description>
      <pubDate>Wed, 23 Jul 2025 13:25:46 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Discussions/Starting-a-fPolicy-server-using-python/m-p/462228#M45083</guid>
      <dc:creator>Aman-Chaurasia</dc:creator>
      <dc:date>2025-07-23T13:25:46Z</dc:date>
    </item>
  </channel>
</rss>

