G
G
Grandma Luda2022-01-04 15:56:48
Python
Grandma Luda, 2022-01-04 15:56:48

Setblocking and settimeout does not work, how to fix it?

Setblocking does not work for me, I replaced it with settimeout, there is no result, I don’t see the proper reasons why it doesn’t work.

The hang occurs in a loop, in the downloats function, when there are a few bytes left before loading

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()

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question