Duet3D Logo Duet3D
    • Tags
    • Documentation
    • Order
    • Register
    • Login

    dsf-python-3.6.0rc1 http endpoint not working for me

    Scheduled Pinned Locked Moved Solved
    DSF Development
    2
    3
    124
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • achrnundefined
      achrn
      last edited by

      I've tried DSF http endpoints on 3.6.0 rc1 and it's not working for me in exactly the same way as it didn't in 3.5.1 (which is described here: https://forum.duet3d.com/topic/35700/dsf-python-3-5-1rc1-problems-with-http-endpoint-not-working?_=1742631997497 but I shall recap):

      Duet 3 MB6HC MB6HC 3.6.0-rc.1
      Duet 3 Expansion TOOL1LC TOOL1LC 3.6.0-rc.1
      Duet Software Framework DSF 3.6.0-rc.1
      Duet Web Control DWC 3.6.0-rc.1
      Raspberry Pi 4 Model B Rev 1.2
      Linux delta.XXXXXX 6.6.74+rpt-rpi-v8 #1 SMP PREEMPT Debian 1:6.6.74-1+rpt1 (2025-01-27) aarch64 GNU/Linux
      from a Bookworm DuetPi image initially

      dsf 3.6 is not currently in the package repository, I got it from https://pypi.org/project/dsf-python/3.6.0rc1/ and did sudo pip3 install --break-system-packages dsf-python==3.6.0rc1

      The custom_http_endpoint.py script does not seem to be in the 3.6.0rc1 source from pypi, but I used the one from 3.5 (I'll add it to the bottom of this post).

      Script output looks like it's working:

      send: {"mode":"Command","version":12}
      recv: {"success":true}
      send: {"command":"AddHttpEndpoint","endpointType":"GET","namespace":"custom","path":"getIt","isUploadRequest":false}
      recv: {"success":true,"result":"/run/dsf/custom/getIt-GET.sock"}
      Try accessing http://duet3.local/machine/custom/getIt in your browser...
      

      as it starts up, then when I make a connection:

      recv: {"sessionId":-1,"queries":{},"headers":{"Accept":"*/*","Connection":"keep-alive","Host":"localhost","User-Agent":"Wget/1.21.3","Accept-Encoding":"identity"},"contentType":null,"body":""}
      send: {"statusCode": 200, "response": "so happy you asked for it!", "responseType": "statuscode"}
      

      However, there's no data received (I used wget on the same machine to bypass any networking shenanigans):

      @delta:~ $  wget -S http://localhost/machine/custom/getIt
      --2025-03-22 09:20:35--  http://localhost/machine/custom/getIt
      Resolving localhost (localhost)... ::1, 127.0.0.1
      Connecting to localhost (localhost)|::1|:80... connected.
      HTTP request sent, awaiting response...
        HTTP/1.1 200 OK
        Content-Length: 0
        Date: Sat, 22 Mar 2025 09:20:35 GMT
        Server: Kestrel
      Length: 0
      Saving to: ‘getIt’
      
      getIt                             [ <=>                                              ]       0  --.-KB/s    in 0s
      
      2025-03-22 09:20:35 (0.00 B/s) - ‘getIt’ saved [0/0]
      

      It has zero content length.

      User running custom_http_endpoint.py is a normal user but member of dsf group.
      Running as root doesn't help.
      Socket has permissions srwxrwxr-x 1 user dsf 0 Mar 22 09:17 getIt-GET.sock

      Journal log looks OK

      Mar 22 09:23:48 delta.XXXXXX DuetWebServer[750]: Microsoft.AspNetCore.Hosting.Diagnostics[1] Request starting HTTP/1.1 GET http://localhost/machine/custom/getIt - - -
      Mar 22 09:23:48 delta.XXXXXX DuetWebServer[750]: DuetWebServer.Singletons.SessionStorage[0] Session 7e5e184ecf1c475fb53104344d27ab60 added (readWrite)
      Mar 22 09:23:48 delta.XXXXXX DuetWebServer[750]: Microsoft.AspNetCore.Hosting.Diagnostics[2] Request finished HTTP/1.1 GET http://localhost/machine/custom/getIt - 200 0 - 23.2772ms
      

      Accessing the URL from a PC on the local network with Edge doesn't get any content either. I haven't tried further browsers (but I did last time).

      This is the endpoint script I'm using:

      #!/usr/bin/env python3
      
      """
      Example to create a custom GET endpoint at http://duet3/machine/custom/getIt
      
      Make sure when running this script to have access to the DSF UNIX socket owned by the dsf user.
      """
      
      import time
      
      from dsf.connections import CommandConnection
      from dsf.http import HttpEndpointConnection
      from dsf.object_model import HttpEndpointType
      
      
      async def respond_something(http_endpoint_connection: HttpEndpointConnection):
          await http_endpoint_connection.read_request()
          await http_endpoint_connection.send_response(200, "so happy you asked for it!")
          http_endpoint_connection.close()
      
      
      def custom_http_endpoint():
          cmd_conn = CommandConnection(debug=True)
          cmd_conn.connect()
      
          # Setup the endpoint
          endpoint = cmd_conn.add_http_endpoint(HttpEndpointType.GET, "custom", "getIt")
          # Register our handler to reply on requests
          endpoint.set_endpoint_handler(respond_something)
      
          print("Try accessing http://duet3.local/machine/custom/getIt in your browser...")
      
          return cmd_conn, endpoint
      
      
      if __name__ == "__main__":
          try:
              cmd_conn, endpoint = custom_http_endpoint()
              # This just simulates doing other things as the new endpoint handler runs async
              time.sleep(1800)
          finally:
              if endpoint is not None:
                  endpoint.close()
              cmd_conn.close()
      
      chrishammundefined 1 Reply Last reply Reply Quote 0
      • chrishammundefined
        chrishamm administrators @achrn
        last edited by

        @achrn said in dsf-python-3.6.0rc1 http endpoint not working for me:

        await http_endpoint_connection.send_response(200, "so happy you asked for it!")

        This call looks wrong to me. I think you need to specify a third parameter like HttpResponseType.PlainText to actually send out data. Otherwise you only return a status code.

        Duet software engineer

        achrnundefined 1 Reply Last reply Reply Quote 0
        • achrnundefined
          achrn @chrishamm
          last edited by

          @chrishamm Thanks! That was it.

          Changed the imports and the send.response() and now it works.

          With hindsight I then find this change (and some others) had been made at https://github.com/Duet3D/dsf-python/blob/v3.6-dev/examples/custom_http_endpoint.py in early December last year, but I had missed that.

          1 Reply Last reply Reply Quote 2
          • chrishammundefined chrishamm marked this topic as a question
          • chrishammundefined chrishamm has marked this topic as solved
          • First post
            Last post
          Unless otherwise noted, all forum content is licensed under CC-BY-SA