【问题标题】:Connect to Google cloud server via python [closed]通过python连接到谷歌云服务器[关闭]
【发布时间】:2021-11-01 16:13:13
【问题描述】:

我想将我的笔记本电脑(以及后来的树莓派/Jetson nano)连接到服务器。目标是能够将数据发送到服务器,然后对其进行处理和评估,并将输出(GPS 坐标)发送回客户端(笔记本电脑/树莓派/Jetson nano)。 理想情况下,我只需将 Google 服务器的公共 IP 地址插入在客户端上运行的代码中并连接到服务器。

但是,运行服务器代码:

import socket

HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.bind((HOST, PORT))
    s.listen()
    conn, addr = s.accept()
    with conn:
        print('Connected by', addr)
        while True:
            data = conn.recv(1024)
            if not data:
                break
            conn.sendall(data)

和客户端代码:

import socket

HOST = '127.0.0.1'  # The server's hostname or IP address
PORT = 65432        # The port used by the server

with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.sendall(b'Hello, world')
    data = s.recv(1024)

print('Received', repr(data))

提供者:https://realpython.com/python-sockets/

仅适用于默认的本地 IP 地址,否则返回 IP 地址找不到错误。

编辑:答案更倾向于通过谷歌云 API 连接互联网。该解决方案完美地说明了这一点,但是我的问题不够明确,抱歉!

【问题讨论】:

  • 你说的改成了google云服务器一号。那个地址是什么?在大多数情况下,您希望监听所有网络接口。该地址是 0.0.0.0。注意:Google Cloud 公共 IP 地址未绑定到网络接口。您不能使用该地址。 127.0.0.1localhost,只能从操作系统内部访问。

标签: python sockets google-cloud-platform google-cloud-functions


【解决方案1】:

有很多方法可以做到这一点,但我建议使用 GCE 实例作为您的服务器,或者使用 App Engine 部署。 Cloud Run 和 Cloud Functions 也可以使用。 (编辑:忘记了 k8s)

请注意,使用 GCE 实例时,您需要按照documentation 打开防火墙。

另外请注意,除非您为 GCE 实例分配静态 IP 地址,否则它将是短暂的。如果您使用 App Engine,则可以在客户端中使用 https://project-id.appspot.com/ 作为服务器地址。

您需要使用 Flask、FastAPI 或其他 webapp 框架设置一个简单的 web 服务器。使用简单的框架可以让生活变得更轻松,因为它会减轻您的负担。

您可以让您的客户端使用它需要作为参数发送的数据执行请求,并让 Web 应用执行魔术并提供响应。我强烈建议返回 JSON 响应,因为这或多或少是 API 的标准。 (您的网络应用程序基本上就是这样)。此外,您可能知道,JSON 很容易转换成 python 中可用的东西。

请看下面这个非常简单的例子。

服务器代码

from flask import Flask, request
import json


app = Flask(__name__)
longitude_divisor = 0.004167


@app.route("/api/gettimediff")
def gettimediff():
    longitude = float(request.args.get("Longitude"))
    # http://www.cs4fn.org/mobile/owntimezone.php
    seconds = longitude / longitude_divisor
    response = json.dumps({"seconds": seconds}, indent=4)
    return response


@app.route("/api/switch")
def switch():
    longitude = float(request.args.get("Longitude"))
    latitude = float(request.args.get("Latitude"))
    response = json.dumps({"Latitude": longitude, "Longitude": latitude})
    return response


if __name__ == "__main__":
    # This is used when running locally only. When deploying to Google App
    # Engine, a webserver process such as Gunicorn will serve the app. This
    # can be configured by adding an `entrypoint` to app.yaml.
    # Flask's development server will automatically serve static files in
    # the "static" directory. See:
    # http://flask.pocoo.org/docs/1.0/quickstart/#static-files. Once deployed,
    # App Engine itself will serve those files as configured in app.yaml.
    app.run(host="127.0.0.1", port=8080, debug=True)

客户端代码

import requests
import json


serveraddress = "http://127.0.0.1:8080/"
data = {"Latitude": 48.85837, "Longitude": 2.294481}


print("switch")
response = requests.get(
    f"{serveraddress}api/switch", 
    params=data
)
print(data)
print(response.text)

print("seconds")
response = requests.get(
    f"{serveraddress}api/gettimediff", 
    params=data
)
print(data)
print(response.text)
converted = json.loads(response.text)
print(converted)

【讨论】:

    猜你喜欢
    • 2017-02-09
    • 1970-01-01
    • 2013-08-16
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-02
    • 2018-06-01
    • 1970-01-01
    相关资源
    最近更新 更多