client code:
import socket
try:
#when using select, i created the read_set somewhere here
s = socket.socket()
s.connect(("localhost", 80))
def receive_bytes(sock):
"""Receives response from server in bytes."""
data = b""
while True:
part = sock.recv(4096)
if not part:
print("Connection closed.")
return None
data += part
if b"\r\n\r\n" in data:
return data
else:
#that was just for troubleshooting
print(data.decode("ISO-8859-1"))
def main():
print("Welcome to the 'list' client. Type a couple words and they will"+
" all be sent back to you.")
active = True
line = ""
while active:
user_message = input("Please enter a word. Type '/quit' to quit. Type 'x' to send.")
if user_message == "/quit":
active = False
elif user_message == "x":
s.sendall(line.encode("ISO-8859-1"))
print("Sending list...")
while True:
response = receive_bytes(s)
if response:
print(response.decode("ISO-8859-1"))
else:
print("Connection closed.")
break
start_again = input("Would you like to try again? [y/N]")
if start_again == "y":
continue
elif start_again.lower() == "n":
break
else:
print("Invalid command.")
else:
user_message = user_message + "\r\n\r\n"
line += user_message
s.close()
if __name__ == "__main__":
main()
except Exception as e:
print(e)
while True:
try_again = input("See message again?: ")
if try_again == "y":
continue
else:
break
server code:
try:
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("localhost", 80))
s.listen()
#only for one client
new_conn = s.accept()
new_socket = new_conn[0]
buffer = b""
def get_next_packet(sock):
global buffer
while True:
#then when using select(), Inside the while loop I did ready_to_read, _, _ = select.select(read_set, [], [])
#i followed the usual, if listening socket, accept (although this server is only for one socket so i just put "pass", else: if ready_to_read[0]: then all the code below went into that clause, however I experienced even worse issues.
if b"\r\n\r\n" in buffer:
delimiter_index = buffer.find(b"\r\n\r\n")
complete_packet = buffer[:delimiter_index+4]
buffer = buffer[delimiter_index+4:]
return complete_packet
#blocking here?
data = sock.recv(4096)
if not data:
print("Connection closed.")
return None
buffer += data
def send_response(info, sock):
decoded = info.decode("ISO-8859-1")
sock.sendall(decoded.encode("ISO-8859-1"))
q = queue.Queue()
while True:
packet = get_next_packet(new_socket)
if not packet:
break
q.put(packet)
print(list(q.queue))
send_response(packet,new_socket)
new_socket.close()
s.close()
except Exception as e:
print(e)
while True:
try_again = input("See message again?: ")
if try_again == "y":
continue
else:
break
I assumed it’s because there was blocking with the sock.recv() part of the server, so I used select, but it was even worse after that, and my server would only receieve the first message.
I then set a timeout for the socket, but even though the server would time out, the client would still just hang I’m not sure why, because I’ve coded it so that if nothing is sent it should break. Maybe I should find a way to return “None”, if no more data needs to be sent for that socket? I’m not sure what approach to take next. Could I have some help? Thanks!