'Python server-client using UDP not receiving messages
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())
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
