{"action":"created","issue":{"url":"https://api.github.com/repos/pohmelie/aioftp/issues/35","repository_url":"https://api.github.com/repos/pohmelie/aioftp","labels_url":"https://api.github.com/repos/pohmelie/aioftp/issues/35/labels{/name}","comments_url":"https://api.github.com/repos/pohmelie/aioftp/issues/35/comments","events_url":"https://api.github.com/repos/pohmelie/aioftp/issues/35/events","html_url":"https://github.com/pohmelie/aioftp/pull/35","id":132958631,"number":35,"title":"[WIP] Added config to restrict port range for passive server","user":{"login":"rsichny","id":745639,"avatar_url":"https://avatars.githubusercontent.com/u/745639?v=3","gravatar_id":"","url":"https://api.github.com/users/rsichny","html_url":"https://github.com/rsichny","followers_url":"https://api.github.com/users/rsichny/followers","following_url":"https://api.github.com/users/rsichny/following{/other_user}","gists_url":"https://api.github.com/users/rsichny/gists{/gist_id}","starred_url":"https://api.github.com/users/rsichny/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rsichny/subscriptions","organizations_url":"https://api.github.com/users/rsichny/orgs","repos_url":"https://api.github.com/users/rsichny/repos","events_url":"https://api.github.com/users/rsichny/events{/privacy}","received_events_url":"https://api.github.com/users/rsichny/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":7,"created_at":"2016-02-11T12:21:21Z","updated_at":"2016-02-11T21:02:16Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/pohmelie/aioftp/pulls/35","html_url":"https://github.com/pohmelie/aioftp/pull/35","diff_url":"https://github.com/pohmelie/aioftp/pull/35.diff","patch_url":"https://github.com/pohmelie/aioftp/pull/35.patch"},"body":"not for merge yet, tbd:\r\n\r\n- [x] change data_ports from list to Queue\r\n- [x] add tests"},"comment":{"url":"https://api.github.com/repos/pohmelie/aioftp/issues/comments/183058744","html_url":"https://github.com/pohmelie/aioftp/pull/35#issuecomment-183058744","issue_url":"https://api.github.com/repos/pohmelie/aioftp/issues/35","id":183058744,"user":{"login":"pohmelie","id":2385765,"avatar_url":"https://avatars.githubusercontent.com/u/2385765?v=3","gravatar_id":"","url":"https://api.github.com/users/pohmelie","html_url":"https://github.com/pohmelie","followers_url":"https://api.github.com/users/pohmelie/followers","following_url":"https://api.github.com/users/pohmelie/following{/other_user}","gists_url":"https://api.github.com/users/pohmelie/gists{/gist_id}","starred_url":"https://api.github.com/users/pohmelie/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pohmelie/subscriptions","organizations_url":"https://api.github.com/users/pohmelie/orgs","repos_url":"https://api.github.com/users/pohmelie/repos","events_url":"https://api.github.com/users/pohmelie/events{/privacy}","received_events_url":"https://api.github.com/users/pohmelie/received_events","type":"User","site_admin":false},"created_at":"2016-02-11T21:02:16Z","updated_at":"2016-02-11T21:02:16Z","body":"@rsichny, got it.\r\n\r\nWhat do you think about this solution:\r\n``` python\r\n    async def _start_passive_server(self, connection, handler_callback):\r\n\r\n        if self.available_data_ports is not None:\r\n\r\n            viewed = set()\r\n            while True:\r\n\r\n                port = await self.available_data_ports.get()                                \r\n                try:\r\n\r\n                    if port in viewed:\r\n\r\n                        raise NoAvailablePort\r\n                        \r\n                    viewed.add(port)\r\n                    passive_server = await asyncio.start_server(\r\n                        handler_callback,\r\n                        connection.server_host,\r\n                        port,\r\n                        loop=connection.loop\r\n                    )\r\n                    connection.passive_server_port = port\r\n                    break\r\n\r\n                except:\r\n\r\n                    self.available_data_ports.put_nowait(port)\r\n                    _, v, _ = sys.exc_info()\r\n                    if not isinstance(v, OSError):\r\n\r\n                        raise\r\n\r\n        else:\r\n\r\n            passive_server = await asyncio.start_server(\r\n                handler_callback,\r\n                connection.server_host,\r\n                connection.passive_server_port,\r\n                loop=connection.loop,\r\n            )\r\n\r\n        return passive_server\r\n```\r\nThis should work fine with queue without priority, since order counts for this implementation. Of course we can miss some ports, that will be put back after our first try.\r\n\r\nOr, maybe there is still better solution… :confused: "}}