Google Cloud Function HTTP 请求连接超时

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

我正在通过以下 Python 代码向 LibreNMS API 执行 http 请求:

import requests
TOKEN = './token.json'
def get_request(request_type):
    url = f'http://192.168.1.141/api/v0/{request_type}'
    with open(TOKEN,'r') as file:
        headers = json.load(file)
    response = requests.get(url,headers=headers)
    if response.status_code == 200:
        pass
    else:
        print(f"Failed to pull for {request_type}: {response.status_code}")
    return response.json()
if __name__ == '__main__':
    dat = get_request('alerts') # pull alerts

该脚本在我的本地计算机上完美运行并且速度相当快。然而,当我尝试将脚本部署为 Google Cloud Function 时,

requests.get(url,headers=headers)
似乎超时了。在该函数的云日志中我得到:

Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.11/site-packages/urllib3/connection.py", line 203, in _new_conn
    sock = connection.create_connection( 
"Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.11/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request("
 "Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.11/site-packages/requests/adapters.py", line 489, in send
    resp = conn.urlopen("
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()"

我在本地计算机和云函数的

requests==2.28.1
文件中安装了相同版本的
urllib3==1.26.14
requirements.txt
。我想问题可能出在这里。

我不确定云功能是否可以对非 Google 服务执行 http 请求。也许这就是问题所在。

请帮助我了解我的功能和/或部署有什么问题。

更新

问题似乎在于地址

192.168.1.141
是本地地址。后来我把它换成了
librenms.bctechnologies.co.za:8050
。然而,这似乎也不公开,因为
response = requests.get(url,headers=headers)
不断超时。

python http google-cloud-platform google-cloud-functions httprequest
1个回答
0
投票

发布 @Guy 评论作为答案,其中运行 LibreNMS 作为私下访问的更好选择。这篇文章的目的是指导其他用户解决这个问题。

补充一下,如果您偶然想继续使用 GCP Cloud VPN 访问您的云功能,您可以通过首先创建一个无服务器 VPC 访问的连接器来实现。

连接器之后,下一步是将您的云功能关联到创建的连接器。您可以通过编辑云函数并按照配置云函数以使用连接器中的步骤来完成此操作。

Cloud Function 使用连接器后,下一步是创建Cloud VPN。只需确保您选择在无服务器 VPC 访问中使用的网络即可连接到您的云功能。

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