通过url将参数传递给python azure函数 [英] Passing arguments to python azure function via url

查看:106
本文介绍了通过url将参数传递给python azure函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过azure函数使hello world示例适用于python.基本功能尝试通过URL检索名称作为输入,然后响应"Hello Name".事实证明,可以通过azure门户使用的示例模板并非开箱即用.基本示例如下:

import os
import json

postreqdata = json.loads(open(os.environ['req']).read())
response = open(os.environ['res'], 'w')
response.write("hello world from "+postreqdata['name'])
response.close()

两个环境变量req和res是临时文件的路径,这些临时文件将函数的输入和输出存储为json.这个想法是通过url传递的输入应该在json.loads()返回的字典中可用.唯一的难题是无论我做什么,位于os.environ ['req']的文件都是空的.

os.path.isfile(os.environ['req'])
# Returns True so the file is located at:
# D:\local\Temp\Functions\Binding\79fcec12-baf3-470e-87c3-113f64ffcef0\req
# during the execution

我也尝试了Hello world JavaScript示例,该示例可直接在azure函数上使用.在azure-portal中执行时,我的python脚本工作正常,但是从Web浏览器触发它时失败.

该函数在Python 2.7.13上运行,扩展名为& name = MyName到https地址.

我相信该错误不在于脚本本身,而是隐藏在主干中的某个位置.有人尝试过吗?

解决方案

您引用的默认示例(来源

要访问URL查询参数,我们将其作为不同的环境变量提供给您.例如,如果发送查询参数"foo",则可以通过os.environ['req_query_foo']进行访问.

I'm trying to make the hello world example work for python via azure functions. The basic function tries to retrieve a name as input through the url and then respond "Hello Name". It turns out that the example-template which is available through the azure portal does not work out of the box. The basic example looks like this:

import os
import json

postreqdata = json.loads(open(os.environ['req']).read())
response = open(os.environ['res'], 'w')
response.write("hello world from "+postreqdata['name'])
response.close()

The two environment variables req and res are paths to temporary files storing input and output from the function as json. The idea is that input passed through the url should be available in the dictionary returned by json.loads(). The only dilemma is that the file located at os.environ['req'] is empty no matter what I do.

os.path.isfile(os.environ['req'])
# Returns True so the file is located at:
# D:\local\Temp\Functions\Binding\79fcec12-baf3-470e-87c3-113f64ffcef0\req
# during the execution

I tried the Hello world JavaScript example as well which works directly out of the box on azure-functions. My python script works fine when executing in within the azure-portal, but fails when triggering it from a web browser.

The function runs on Python 2.7.13 with extension &name=MyName to the https adress.

I believe the bug is not in the script itself, but hidden in the backbone somewhere. Anyone tried the same?

解决方案

The default sample you refer to (source here) accepts an http POST request, and requires your request body to be a JSON payload with a "name" property.

To access URL query parameters, we make them available to you as distinct environment variables. For example, if you send query parameter "foo", you can access it via os.environ['req_query_foo'].

这篇关于通过url将参数传递给python azure函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆