找不到eventlet.green.urllib2模块

时间:2017-11-08 04:30:07

标签: python eventlet

我正在尝试在http://eventlet.net/doc/examples.htmlwebcrawler.py上运行第一个代码示例:

#!/usr/bin/env python
"""
This is a simple web "crawler" that fetches a bunch of urls using a pool to
control the number of outbound connections. It has as many simultaneously open
connections as coroutines in the pool.

The prints in the body of the fetch function are there to demonstrate that the
requests are truly made in parallel.
"""
import eventlet
from eventlet.green import urllib2


urls = [
    "https://www.google.com/intl/en_ALL/images/logo.gif",
    "http://python.org/images/python-logo.gif",
    "http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif",
]


def fetch(url):
    print("opening", url)
    body = urllib2.urlopen(url).read()
    print("done with", url)
    return url, body


pool = eventlet.GreenPool(200)
for url, body in pool.imap(fetch, urls):
    print("got body from", url, "of length", len(body))

但是,这会导致

ModuleNotFoundError: No module named 'urllib2'

我正在使用eventlet版本0.21.0。该模块是否在eventlet内移动?

3 个答案:

答案 0 :(得分:1)

我终于将我的构建系统(在Sublime Editor中)切换到Python 2而不是Python 3.现在它按预期运行:

('opening', 'https://www.google.com/intl/en_ALL/images/logo.gif')
('opening', 'http://python.org/images/python-logo.gif')
('opening', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif')
('done with', 'https://www.google.com/intl/en_ALL/images/logo.gif')
('got body from', 'https://www.google.com/intl/en_ALL/images/logo.gif', 'of length', 8558)
('done with', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif')
('done with', 'http://python.org/images/python-logo.gif')
('got body from', 'http://python.org/images/python-logo.gif', 'of length', 2549)
('got body from', 'http://us.i1.yimg.com/us.yimg.com/i/ww/beta/y3.gif', 'of length', 1874)
[Finished in 0.8s]

顺便说一下,这表明绿色线程按预期异步运行。

答案 1 :(得分:0)

eventlet.green包模仿Python stdlib模块层次结构。

Python2有urllib2模块。 Python3包含带有详细子模块的urllib包。

一般想法:查看普通的Python代码,使用eventlet.green版本或mod = eventlet.import_patched('mod')更改阻止模块导入并享受。

答案 2 :(得分:0)

这些示例似乎已经过时了(现在更是如此)。

对于python 2,使用此命令:

import eventlet
from eventlet.green import urllib2 as request

request.urlopen(...)

对于python 3,使用此命令:

import eventlet
from eventlet.green.urllib import request

request.urlopen(...)