Python 和 GnuPG 调用中的 Azure 函数

问题描述 投票:0回答:1

我有一个 Python 中的 Azure 函数,我正在尝试使用 python-gnupg 包装器在进行本地开发时调用 GnuPG 二进制文件。

这是我在 Azure 函数内部使用 HTTP 触发器尝试的代码。

import gnupg
import tempfile
import subprocess
import azure.functions as func
import logging

@app.route(route="PGPOne")
def PGPOne(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')

# Correctly obtaining the GPG binary path
gpg_path = r'C:\PGPD\dependencies\gpg.exe'

# Testing the GPG binary works
result = subprocess.run([gpg_path, '--version'], capture_output=True, text=True)
print(result.stdout)

# Creating a temporary directory for GPG home
temp_dir = tempfile.mkdtemp()
print(f"Temporary GPG home directory: {temp_dir}")

# Initializing GPG with the temporary home directory
gpg = gnupg.GPG(homedir=temp_dir, binary=gpg_path)

name = req.params.get('name')
if not name:
    try:
        req_body = req.get_json()
    except ValueError:
        pass
    else:
        name = req_body.get('name')

if name:
    return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
else:
    return func.HttpResponse(
         "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
         status_code=200

代码块 [测试 GPG 二进制文件是否有效] 和 [为 GPG 主目录创建临时目录] 都按预期工作,并且我得到了相应打印语句的以下输出。

Temporary directory: C:\Users\<myusername>\AppData\Local\Temp\tmpxacvv8_i
GPG binary path: C:\PGPD\dependencies\gpg.exe

但是调用

gnupg.GPG(homedir=temp_dir, binary=gpg_path)

导致以-

开头的错误
Python HTTP trigger function processed a request.
[2024-03-18T04:13:19.620Z] Creating directory: C:\PGPD\'C:\Users\<myusername>\AppData\Local\Temp\tmpxacvv8_i'
[2024-03-18T04:13:19.658Z] [WinError 123] The filename, directory name, or volume label syntax is incorrect: "C:\\PGPD\\'C:"

为什么在创建目录时这部分在调用中被加上前缀:

 C:\PGPD\'

我做错了什么以及如何纠正?


这是在使用 Function Core Tools 本地调试函数并在 VS Code 内的虚拟环境设置中使用 Python 3.10 时进行的。

我已按照 Microsoft 文档的建议将 GnuPG 二进制依赖项引入到代码文件夹结构中。

python azure azure-functions gnupg python-gnupgp
1个回答
0
投票

对我有用的

code
如下,您可以集成到azure功能中,我遵循了文档

import gnupg
import tempfile
import os
import subprocess as rithsb

rithgpg_path = r'C:\Program Files (x86)\GnuPG\bin\gpg.exe'
oute = rithsb.run([rithgpg_path, '--version'], capture_output=True, text=True)
print(oute.stdout)
rith_temp_dir = tempfile.mkdtemp()
print(f"Temporary GPG home directory: {rith_temp_dir}")
gpg = gnupg.GPG(gnupghome=os.path.abspath(rith_temp_dir))
print(gpg)

Output:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.