У меня не работает setblocking я заменила на settimeout результата нет, должных причин почему не работает не вижу.
Зависание происходит в цикле, в функции downloats когда до загрузки остаётся несколько байт
import socket, asyncio
class Server():
def __init__(self, format_of_the_conference, path_seving):
self.socket = socket.socket(
socket.AF_INET,
socket.SOCK_STREAM,
)
self.path_seving = path_seving
self.format_of_the_conference = format_of_the_conference
self.main_loop = asyncio.new_event_loop()
def set_up(self, ip):
self.socket.bind((ip, 5845))
self.socket.listen(10)
self.socket.setblocking(False)
print("Server is listening")
async def install(self, file_name, user=None):
if not user:
return
file = open(self.path_seving + "\\" + file_name, "rb")
await self.main_loop.sock_sendfile(user, file, offset=0)
print(1)
async def downloats(self, file_name, user=None):
if not user:
return
file = open(self.path_seving + "\\" + file_name, "wb")
data = await self.main_loop.sock_recv(user, 1024)
while True:
file.write(data)
data = await self.main_loop.sock_recv(user, 1024)
if not data:
print("er")
break
file.close()
async def command_identifier(self, user=None):
if not user:
return
command = await self.main_loop.sock_recv(user, 2048)
file_name = await self.main_loop.sock_recv(user, 2048)
if command == b"downloats":
self.main_loop.create_task(self.downloats(file_name.decode(self.format_of_the_conference),user))
elif command == b"install":
self.main_loop.create_task(self.install(file_name.decode(self.format_of_the_conference),user))
async def accept_socket(self):
while True:
user_socket, address = await self.main_loop.sock_accept(self.socket)
self.main_loop.create_task(self.command_identifier(user_socket))
def start(self):
self.main_loop.run_until_complete(self.main())
async def main(self):
await self.main_loop.create_task(self.accept_socket())
server = Server('Windows-1251','files')
server.set_up("192.168.211.1")
server.start()