API 调用脚本在 cron 作业中失败,但在手动运行时工作正常

API call script fails in cron job but works fine when run manually

提问人:Flanimal 提问时间:11/16/2023 最后编辑:Brian Tompsett - 汤莱恩Flanimal 更新时间:11/16/2023 访问量:51

问:

API 调用脚本在 cron 作业中失败,但在手动运行时工作正常。 我的 Python 脚本正在调用 https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema 我得到的错误是:

请求失败:HTTPSConnectionPool(host='stat-xplore.dwp.gov.uk', port=445):超出最大重试次数,网址:/webapi/rest/v1/schema(由 ConnectTimeoutError(<urllib3.connection.VerifiedHTTPSConnection 对象在 0j0iefhfnfj9>导致,“与 stat-xplore.dwp.gov.uk 的连接超时。(连接超时=30)'))

我试过增加超时,但没有区别。不涉及使 API 调用正常工作的 Cron 作业。

#!/usr/bin/python3
import json
import requests
import os


url = 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema'

# Set your API key in the environment variable
os.environ['APIKey'] = 'myapikey'

#I have also tried this without setting environment variable
#api_key='myapikey'

# Get the API key from the environment variable
api_key = os.environ['APIKey']
headers = {'Accept': 'application/json', 'APIKey': api_key}

try:
    response = requests.get(url, headers=headers, timeout=30)
    print(response)
    # Process the response...
except requests.RequestException as e:
    print("Request failed:", e)

我的 cron 工作

42 18 * * * /usr/bin/python3 /home/user/bin/bash/statxplore_api.py

我尝试过的事情:

设置环境变量, 检查文件权限以确保可以执行, 通过ping到 https://stat-xplore.dwp.gov.uk 测试网络连接

蟒蛇 cron

评论


答:

0赞 Flanimal 11/16/2023 #1

我通过在脚本中添加代理来解决这个问题。出现此问题的原因是我的用户在我的配置文件下安装了代理,但 contab 没有。

#!/usr/bin/python3

import json
import requests
import os


# Define your proxy server
proxies = {
    'http': 'http://yourproxy:0303',
    'https': 'http://yourproxy:0303'
}


url = 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema'

# Set your API key in the environment variable
os.environ['APIKey'] = 'myapikey'

# Get the API key from the environment variable
api_key = os.environ['APIKey']
headers = {'Accept': 'application/json', 'APIKey': api_key}

try:
    response = requests.get(url, headers=headers, proxies=proxies, timeout=30)
    print(response)
    # Process the response...
except requests.RequestException as e:
    print("Request failed:", e)

要了解正在使用的代理,您可以运行以下代码

import os

proxy_env_vars = ['http_proxy', 'https_proxy']

# Check for proxy settings
for var in proxy_env_vars:
    proxy_val = os.getenv(var)
    if proxy_val:
        print(f"{var}: {proxy_val}")
    else:
        print(f"{var}: Not set")