Test Python Google Cloud Functions locally

后端 未结 4 1581
一个人的身影
一个人的身影 2021-02-01 04:24

I\'m trying out the Python3.7 runtime on Google Cloud Functions. I am able to deploy the functions and make them work once deployed, however, I can\'t seem to run the emulator t

4条回答
  •  暖寄归人
    2021-02-01 04:52

    You can use the Functions Framework for Python to run the function locally.

    Given a function in a file named main.py like so:

    def my_function(request):
        return 'Hello World'
    

    You can do:

    $ pip install functions-framework
    $ functions-framework --target my_function
    

    Which will start a local development server at http://localhost:8080.

    To invoke it locally for an HTTP function:

    $ curl http://localhost:8080
    

    For a background function with non-binary data:

    $ curl -d '{"data": {"hi": "there"}}' -X POST \
    -H "Content-Type: application/json" \
    http://localhost:8080
    

    For a background function with binary data:

    $ curl -d "@binary_file.file" -X POST \
    -H "Ce-Type: true" \
    -H "Ce-Specversion: true" \
    -H "Ce-Source: true" \
    -H "Ce-Id: true" \
    -H "Content-Type: application/json" \
    http://localhost:8080
    

提交回复
热议问题