Не передаются данные при помощи socket
Решил сделать P2P обменник сообщениями, есть 2 файла:
import socket
import stun
port = int(input("port to bind: "))
client_get = socket.socket()
client_get.bind(("127.0.0.1", port))
client_get.listen(10)
print("client_get starts")
while True:
con, addr = client_get.accept()
print(f"{addr} conected")
data = con.recv(1024)
message = data.decode()
match message:
case "###client want deconect###":
print("###client want deconect###")
con.close()
print("client deconected")
case "###get_info_file###":
data = con.recv(1024)
print(data)
geted_info_file = eval(data)
print(geted_info_file)
match geted_info_file["type"]:
case "sys":
match geted_info_file["msg"]:
case "fl_send":
size = geted_info_file["size"]
print(size)
try:
f = open(geted_info_file["fl_name"], "wb")
print("wb")
i = size
while i != 0:
print(i)
dataf = con.recv(1024)
print("dataf")
print(bytes.decode(dataf))
f.write(dataf)
i -= 1
print("g")
finally:
try:
f.close()
print("c")
finally:
continue
print("f")
continue
if message == "###client want deconect###":
print("###client want deconect###")
con.close()
print("client deconected")
И второй файл:
import socket
import os
config = {"connection": {"connection_address": "", "port": 0}}
form_to_send = {
"type": "sys",
"msg": "fl_send",
"size": 1,
"fl_name": "gigy.txt",
}
client_send = socket.socket()
connection_name = ""
port = ""
while True:
command = input(": ")
match command:
case "info":
print(f"connection_name: {connection_name} \n" f"port: {port}")
case "help":
print(
"info - information\n"
"help - help information\n"
"conconfig - switch connection config\n"
"startcon - start connection\n"
"endcon - end connection\n"
)
case "conconfig":
connection_name = input("new connection name: ")
port = int(input("new port: "))
case "startcon":
client_send.connect((connection_name, port))
print("conected")
case "endcon":
message = "###client want deconect###"
client_send.send(message.encode())
client_send.close()
print("closed")
case "send":
message = "###get_info_file###"
client_send.send(message.encode())
f = open("gigy.txt", "rb")
size = os.path.getsize("gigy.txt")
file_txt = f.read(size)
size_to_send = size // 1024
if size % 1024 != 0:
size_to_send += 1
form_to_send["size"] = size_to_send
message = str(form_to_send)
client_send.send(message.encode())
i = size
while i != 0:
message = f.read(1024)
client_send.send(message)
i -= 1
По неизвестной мне причине в момент который помечен как dataf
пакет не принимается.
Источник: Stack Overflow на русском