Dude
Dude

Reputation: 145

How to send a Python Get request to a local webserver (running in Houdini)

I am experimenting with a Python module to run an internal webserver in a 3D DCC (Houdini). Docs: https://www.sidefx.com/docs/houdini/hwebserver/index.html

There is an example provided in the docs to set up a server to do some basic image processing when sent a get request (https://www.sidefx.com/docs/houdini/hwebserver/Request.html):

import tempfile
import hwebserver
import hou


@hwebserver.urlHandler("/blur_image")
def blur_image(request):
    if request.method() == "GET":
        return hwebserver.Response('''
            <p>Upload an image</p>
            <form method="POST" enctype="multipart/form-data">
                <input type="file" name="image_file">
                <input type="submit">
            </form>
        ''')

    if request.method() != "POST":
        return hwebserver.notFoundResponse(request)

    image_file = request.files().get("image_file")
    if image_file is None:
        return hwebserver.errorResponse(request, "No image was posted", 422)
    image_file.saveToDisk()

    # Use a COP network to load the image, blur it, and write it to a
    # temporary output file.
    copnet = hou.node("/img").createNode("img")
    file_cop = copnet.createNode("file")
    file_cop.parm("filename1").set(image_file.temporaryFilePath())

    blur_cop = copnet.createNode("blur")
    blur_cop.setFirstInput(file_cop)
    blur_cop.parm("blursize").set(10)

    rop = copnet.createNode("rop_comp")
    rop.setFirstInput(blur_cop)
    rop.parmTuple("f").set((1, 1, 1))
    temp_output_file = tempfile.mkstemp(".jpg")[1]
    rop.parm("copoutput").set(temp_output_file)
    rop.render()

    copnet.destroy()

    return hwebserver.fileResponse(temp_output_file, delete_file=True)


hwebserver.run(8008, True)

I want to test this out locally on the same machine that I am running the server on Houdini.
What is the correct way to send the get request using python?

My current code is:

import requests

resp = requests.get("http://127.0.0.1:8008")

I am running http://127.0.0.1:8008 in a browser to check for a response. But nothing seems to happen.. what am I missing here?

Thanks for the advice.

Upvotes: 0

Views: 202

Answers (1)

Dude
Dude

Reputation: 145

I have now got this to work, I was not taking into account the path in:

@hwebserver.urlHandler("/blur_image")

Upvotes: 0

Related Questions