如何保护socket传输的数据?

发布于 2025-01-13 19:36:37 字数 931 浏览 0 评论 0原文

此代码用于在我的 PC 和 AWS 实例之间发送和接收 我想知道传输的数据是否安全?或者我是否需要其他东西来确保传输数据的安全? 如果数据未加密,需要添加什么代码来确保数据安全?

客户端代码

import socket

HOST = "public ip of server"

PORT = 4444  # 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(f"Received {data!r}")

服务器代码

服务器代码

import socket



HOST = "0.0.0.0" 

PORT = 4444  #open this in your router 



with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

    s.bind((HOST, PORT))

    s.listen()

    conn, addr = s.accept()

    with conn:

        print(f"Connected by {addr}")

        while True:

            data = conn.recv(1024)

            if not data:

                break

            conn.sendall(data)

This code is for sending and receiving between my PC and AWS Instance
I want to know whether the transmitted data is considered secure? or do I need something else to ensure the security of the transmitted data?
If the data is not encrypted, what is the addition to the code to make it secure?

code's client

import socket

HOST = "public ip of server"

PORT = 4444  # 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(f"Received {data!r}")

server's code

server code

import socket



HOST = "0.0.0.0" 

PORT = 4444  #open this in your router 



with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:

    s.bind((HOST, PORT))

    s.listen()

    conn, addr = s.accept()

    with conn:

        print(f"Connected by {addr}")

        while True:

            data = conn.recv(1024)

            if not data:

                break

            conn.sendall(data)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文