Не получается отправить файл через сокет
У меня есть скрипт для обмена файлами между сервером и клиентом main.py
def main():
s.bind((HOST, PORT))
s.listen(10)
print('server listening on port {}...'.format(PORT))
conn, _ = s.accept()
while True:
cmd = input('> ').rstrip()
if cmd == '':
continue
conn.send(bytes(cmd, encoding="utf-8", errors="ignore"))
if cmd == 'exit':
s.close()
sys.exit(0)
if cmd[:7] == "getfile":
f = open("FILENAME.txt", "wb")
while True:
data = conn.recv(4096)
if not data:
f.write(data)
break
f.close()
print('Done sending\n')
data = conn.recv(4096)
print(str(data, encoding="utf-8", errors="ignore"))
client.py
def session():
while True:
data = s.recv(1024)
cmd = str(data, encoding="utf-8", errors="ignore")
if cmd == 'shutdown':
s.close()
exit(0)
elif cmd[:7] == "getfile":
try:
f = open(cmd[8:], "rb")
data_to_send = f.read()
s.send(bytes(data_to_send))
f.close()
s.send(bytes("\nFile has been sent\n" + "\n", encoding="utf-8", errors="ignore"))
except Exception as ex:
s.send(bytes("Error:\n" + str(ex) + "\n", encoding="utf-8", errors="ignore"))
но при вводе команды getfile 123.txt скрипт попадает в бесконечный while Тrue, то есть if not data не работает
как я могу это исправить?