使用UDP未接收消息的Python Server-Client

发布于 2025-01-28 08:07:47 字数 1380 浏览 1 评论 0原文

因此,我正在尝试制作一个Python程序,该程序将文件从客户端传输到服务器,反之亦然,首先,我正在测试一个简单的情况,其中包括从客户端到服务器的简单消息,客户客户端发送消息:“获取文件”,服务器将获取。此消息并发送回另一个消息。问题是客户端发送消息,服务器接收并发送响应,但客户端没有得到它。

server.py

import time

host = 'localhost'
port = 8080
server_address = (host, port)
socket = sk.socket(sk.AF_INET, sk.SOCK_DGRAM)
socket.bind(server_address)

get_files = "get files"

while True:
    print("Waiting for message to receive")
    data, address = socket.recvfrom(4096)
    if data:
        if data.decode() == get_files:
            print("Getting the list of files present")
            time.sleep(1)
            list_files = "List of files"
            send = socket.sendto(list_files.encode(), server_address)
        else:
            break
    else:
        break

client.py

import time

host = 'localhost'
port = 8080
socket = sk.socket(sk.AF_INET, sk.SOCK_DGRAM)
server_address = (host, port)

action = input()

if action == "get files":
    while True:
        print("Sending request to server")
        time.sleep(2)
        send = socket.sendto(action.encode(), server_address)
        print("Request sent. Waiting for response")
        data, server = socket.recvfrom(4096)
        time.sleep(2)
        if data:
            print("List of files present on server:")
            print(data.decode())

So I am trying to make a python program which transfers files from client to server and vice-versa and firstly I am testing a simple case with simple messages from client to server, where client sends a message: "get files" and server will get this message and send back another message. The problem is that the client sends the message, server receives it and sends the response but client doesn't get it.

server.py

import time

host = 'localhost'
port = 8080
server_address = (host, port)
socket = sk.socket(sk.AF_INET, sk.SOCK_DGRAM)
socket.bind(server_address)

get_files = "get files"

while True:
    print("Waiting for message to receive")
    data, address = socket.recvfrom(4096)
    if data:
        if data.decode() == get_files:
            print("Getting the list of files present")
            time.sleep(1)
            list_files = "List of files"
            send = socket.sendto(list_files.encode(), server_address)
        else:
            break
    else:
        break

client.py

import time

host = 'localhost'
port = 8080
socket = sk.socket(sk.AF_INET, sk.SOCK_DGRAM)
server_address = (host, port)

action = input()

if action == "get files":
    while True:
        print("Sending request to server")
        time.sleep(2)
        send = socket.sendto(action.encode(), server_address)
        print("Request sent. Waiting for response")
        data, server = socket.recvfrom(4096)
        time.sleep(2)
        if data:
            print("List of files present on server:")
            print(data.decode())

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

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

发布评论

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