Python - aiohttp - 为什么我的测试不会 运行?

Python - aiohttp - Why won't my tests run?

我已经在这里下载了代码 https://github.com/aosabook/500lines

这是一个有很多子项目的教育开源项目。我目前正在研究的是'crawler'项目。它需要 python 3.4+ 和 aiohttp 1.2+。我当前的设置是 Windows 10、Python 3.6.1 和 aiohttp 2.0.4。

奇怪的是,我的测试没有运行,它与 aiohttp 有关。熟悉 aiohttp 的人能告诉我为什么吗?

这是我得到的输出:

C:\Users\Primary User\Desktop0lines-master\crawler\code>python test.py
C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web.py:54: ResourceWarning: loop argument is deprecated
  warnings.warn("loop argument is deprecated", ResourceWarning)
E.E..EEEEEEEEEE..
======================================================================
ERROR: test_content_type (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 314, in test_content_type
    self.add_page(content_type='foo')
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_encoding (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 309, in test_encoding
    test_charset('', 'utf-8')
  File "test.py", line 305, in test_charset
    self.add_page(url, content_type=content_type)
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_link (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 126, in test_link
    self.add_page('/', ['/foo'])
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_link_cycle (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 137, in test_link_cycle
    url = self.add_page('/foo', ['/bar'])
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_max_tasks (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 264, in test_max_tasks
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_max_tries (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 281, in test_max_tries
    self.add_handler('/', handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_non_html (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 322, in test_non_html
    self.add_page('/xml', body=body, content_type='application/xml')
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_non_http (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 333, in test_non_http
    self.add_page(body=body)
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_prohibited_host (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 152, in test_prohibited_host
    self.add_page('/', ['http://example.com'])
  File "test.py", line 93, in add_page
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_redirect (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 196, in test_redirect
    url = self.add_redirect('/', foo)
  File "test.py", line 101, in add_redirect
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_redirect_cycle (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 214, in test_redirect_cycle
    url = self.add_redirect('/bar', foo)
  File "test.py", line 101, in add_redirect
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

======================================================================
ERROR: test_redirect_join (__main__.TestCrawler)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "test.py", line 230, in test_redirect_join
    self.add_redirect('/foo', baz)
  File "test.py", line 101, in add_redirect
    self.add_handler(url, handler)
  File "test.py", line 76, in add_handler
    self.app.router.add_route('GET', url, handler)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 823, in add_route
    resource = self.add_resource(path, name=name)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 787, in add_resource
    self.register_resource(resource)
  File "C:\Users\Primary User\AppData\Local\Programs\Python\Python36-32\lib\site-packages\aiohttp\web_urldispatcher.py", line 762, in register_resource
    "Cannot register a resource into frozen router.")
RuntimeError: Cannot register a resource into frozen router.

----------------------------------------------------------------------
Ran 17 tests in 0.034s

FAILED (errors=12)

C:\Users\Primary User\Desktop0lines-master\crawler\code>

我也在学习。我发现了这个问题 https://github.com/hangoutsbot/hangoutsbot/pull/655。然后这样编码

@asyncio.coroutine
def _create_server(self):
    app = web.Application(loop=self.loop)
    return app

def add_handler(self, url, handler):
    self.app.router.add_route('GET', url, handler)
    handler_factory = self.app.make_handler(debug=True)
    srv = yield from self.loop.create_server(
        handler_factory, '127.0.0.1', self.port)

    # Prevent "Task destroyed but it is pending" warnings.
    self.addCleanup(lambda: self.loop.run_until_complete(
        handler_factory.finish_connections()))

    self.addCleanup(srv.close)

我认为你应该 运行 crawl.py。另一个问题,aiohttp 2.0.4 在我 运行ning 之后没有结果。使用 aiohttp 0.21 修复了它。