该元素指定将在 API 流中执行的主要 Python 文件。您可以在 API 代理范围(API 代理软件包的 /apiproxy/resources/py 下方或 API 代理编辑器导航工具窗格的“脚本”部分中)或组织或环境范围中存储此文件,以便在多个 API 代理中重复使用,如资源文件中所述。您的代码可以使用 JavaScript 对象模型的对象、方法和属性。
{"fault":{"faultstring":"Execution of SetResponse failed with error: Pythonscript runtime error: "ReferenceError:"status"isnotdefined.\"","detail":{"errorcode":"steps.script.ScriptExecutionFailed"}}}
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-05。"],[[["\u003cp\u003eThe Python Script policy in Apigee and Apigee hybrid allows users to add custom Python code to their API proxy flows, extending beyond the capabilities of standard Apigee policies.\u003c/p\u003e\n"],["\u003cp\u003ePython scripts are executed through Jython version 2.5.2, and any added third-party libraries must be "pure Python," and added through Resource files.\u003c/p\u003e\n"],["\u003cp\u003eA Python policy does not contain code; instead, it references a Python resource file (specified via the \u003ccode\u003e<ResourceURL>\u003c/code\u003e element) and defines where the script executes in the API flow.\u003c/p\u003e\n"],["\u003cp\u003eThe policy supports including dependent Python files via the \u003ccode\u003e<IncludeURL>\u003c/code\u003e element, allowing for modular code organization, and scripts are evaluated in the order listed in the policy.\u003c/p\u003e\n"],["\u003cp\u003eThe policy provides detailed error codes and fault variables for both runtime and deployment errors, aiding in troubleshooting and the creation of custom fault rules for error handling.\u003c/p\u003e\n"]]],[],null,["# PythonScript policy\n\n*This page\napplies to **Apigee** and **Apigee hybrid**.*\n\n\n*View [Apigee Edge](https://docs.apigee.com/api-platform/get-started/what-apigee-edge) documentation.*\n\n### What\n\nThe Python Script policy lets you add customized Python functionality to your API proxy flow,\nespecially when the functionality you need is beyond what the Apigee out-of-the-box policies\nprovide.\n\nThis policy is an *Extensible policy* and use of this policy might have cost or\nutilization implications, depending on your Apigee license. For information on policy types\nand usage implications, see\n[Policy types](/apigee/docs/api-platform/reference/policies/reference-overview-policy#policy-types).\n\nPython language support is provided through [Jython\nversion 2.5.2](https://www.jython.org/jython-old-sites/docs/index.html). Third-party libraries you add must be \"pure Python\" (implemented only in Python).\nFor more on adding libraries, see [Resource files](/apigee/docs/api-platform/develop/resource-files).\n\n\n| **Note:** This feature may not be available in all Apigee plans. To see which pricing plan includes this feature, see \u003chttps://cloud.google.com/apigee/pricing/\u003e.\n\n\u003cbr /\u003e\n\nA Python policy contains no actual code. Instead, a Python policy references a Python\nresource and defines the Step in the API flow where the Python script executes. You can upload\nyour script through the Apigee UI proxy editor, or you can include it in the\n`/resources/py` directory in API proxies that you develop locally.\n| **WARNING:** \n| You should limit or disable SSH access to your security resources to prevent this policy from being used to access internal files, current user info, the process list, or CPU/memory utilization.\n\n### Samples\n\n### Python policy \\& script\n\n### Python Script policy\n\n```python\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?\u003e\n\u003cScript name=\"Python-1\"\u003e\n \u003cDisplayName\u003ePython-1\u003c/DisplayName\u003e\n \u003cResourceURL\u003epy://myscript.py\u003c/ResourceURL\u003e\n\u003c/Script\u003e\n```\n\nIn this example, the element, ResourceURL specifies the relevant Python script\nresource.\n\n### Python Script\n\nThis shows what you might include in the Python script itself. \n\n```python\nimport base64\n\nusername = flow.getVariable(\"request.formparam.client_id\")\npassword = flow.getVariable(\"request.formparam.client_secret\")\n\nbase64string = base64.encodestring('%s:%s' % (username, password))[:-1]\nauthorization = \"Basic \"+base64string\n\nflow.setVariable(\"authorizationParam\",authorization)\n```\n\nElement reference\n-----------------\n\n```python\n\u003c?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?\u003e\n\u003cScript name=\"Python-1\"\u003e\n \u003cDisplayName\u003ePython-1\u003c/DisplayName\u003e\n \u003cResourceURL\u003epy://myscript.py\u003c/ResourceURL\u003e\n \u003cIncludeURL\u003epy://myscript_dependency.py\u003c/IncludeURL\u003e\n\u003c/Script\u003e\n```\n\nThe following table describes attributes that are common to all policy parent elements:\n\n\\\u003cDisplayName\\\u003e element\n-----------------------\n\nUse in addition to the `name` attribute to label the policy in the\nmanagement UI proxy editor with a different, natural-language name. \n\n```text\n\u003cDisplayName\u003ePolicy Display Name\u003c/DisplayName\u003e\n```\n\n\\\u003cResourceURL\\\u003e\nelement\n-----------------------\n\nThis element specifies the main Python file that will execute in the API flow. You can store\nthis file at the API proxy scope (under `/apiproxy/resources/py` in the API proxy\nbundle or in the Scripts section of the API proxy editor's Navigator pane), or at the\norganization or environment scopes for reuse across multiple API proxies, as described in\n[Resource files](/apigee/docs/api-platform/develop/resource-files). Your code can use the\nobjects, methods, and properties of the [JavaScript object model](/apigee/docs/api-platform/reference/javascript-object-model). \n\n```python\n\u003cResourceURL\u003epy://myscript.py\u003c/ResourceURL\u003e\n```\n\n\\\u003cIncludeURL\\\u003e\nelement\n----------------------\n\nSpecifies a Python file to be loaded as dependency to the main Python file specified with the\n`\u003cResourceURL\u003e` element. The scripts will be evaluated in the order in which\nthey are listed in the policy.\n\nInclude more than one Python dependency resource with additional\n`\u003cIncludeURL\u003e` elements.\n**Warning:** If your Python files are stored at the proxy or environment level, be sure they were uploaded correctly with cURL using the `-F` option or as a file attachment through a REST client. Content-Type is `multipart/form-data`. For more information, see [Resource files](/apigee/docs/api-platform/develop/resource-files). \n\n```python\n\u003cIncludeURL\u003epy://myscript_dependency.py\u003c/IncludeURL\u003e\n```\n\nError codes\n-----------\n\n\nThis section describes the fault codes and error messages that are returned and fault variables\nthat are set by Apigee when this policy triggers an error. This information is important to know\nif you are developing fault rules to\nhandle faults. To learn more, see [What you need to know\nabout policy errors](/apigee/docs/api-platform/fundamentals/what-you-need-know-about-policy-errors) and [Handling\nfaults](/apigee/docs/api-platform/fundamentals/fault-handling).\n\n### Runtime errors\n\nThese errors can occur when the policy executes.\n| **Tip:** Need help resolving an error? Click *build* in the Fix column for detailed troubleshooting information.\n\n### Deployment errors\n\nThese errors can occur when you deploy a proxy containing this policy.\n| **Tip:** Need help resolving an error? Click *build* next to the error's cause description to visit the troubleshooting page.\n\n### Fault variables\n\nThese variables are set when this policy triggers an error at runtime. For more information,\nsee [What you\nneed to know about policy errors](/apigee/docs/api-platform/fundamentals/what-you-need-know-about-policy-errors).\n\n### Example error response\n\n**Note:** For error handling, the best practice is to trap the `errorcode` part of the error response. Do not rely on the text in the `faultstring`, because it could change. \n\n```python\n{\n \"fault\": {\n \"faultstring\": \"Execution of SetResponse failed with error: Pythonscript runtime error: \"ReferenceError: \"status\" is not defined.\\\"\",\n \"detail\": {\n \"errorcode\": \"steps.script.ScriptExecutionFailed\"\n }\n }\n}\n```\n\n### Example fault rule\n\n```python\n\u003cFaultRule name=\"PythonScript Policy Faults\"\u003e\n \u003cStep\u003e\n \u003cName\u003eAM-CustomErrorResponse\u003c/Name\u003e\n \u003cCondition\u003e(fault.name Matches \"ScriptExecutionFailed\") \u003c/Condition\u003e\n \u003c/Step\u003e\n \u003cCondition\u003e(pythonscript.PythonScript-1.failed = true) \u003c/Condition\u003e\n\u003c/FaultRule\u003e\n```\n\n\u003cbr /\u003e"]]