使用UDP未接收消息的Python Server-Client
因此,我正在尝试制作一个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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论