V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
yeelone
V2EX  ›  Python

python gevent 报:urlopen error 错误,求救啊!!

  •  
  •   yeelone · 2013-06-25 15:52:49 +08:00 · 4698 次点击
    这是一个创建于 4160 天前的主题,其中的信息可能已经有所发展或是发生改变。
    以下是看教程中一个例子:

    import gevent.monkey
    gevent.monkey.patch_all()

    import gevent
    import urllib2

    def fetch(pid):
    response = urllib2.urlopen("http://www.baidu.com")
    result = response.read()

    print pid

    def synchronous():
    for i in range(1,10):
    fetch(i)

    def asynchronous():
    threads = []
    for i in range(1,10):
    threads.append(gevent.spawn(fetch,i))
    gevent.joinall(threads)


    print 'synchronous:'
    synchronous()

    print 'Asynchronous:'
    asynchronous()


    会报如下错误 :
    ubuntu@yee:/tmp$ python t3.py
    synchronous:
    Traceback (most recent call last):
    File "t3.py", line 26, in <module>
    synchronous()
    File "t3.py", line 16, in synchronous
    fetch(i)
    File "t3.py", line 9, in fetch
    response = urllib2.urlopen("http://www.baidu.com")
    File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
    File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
    File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
    File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
    File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
    File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
    urllib2.URLError: <urlopen error [Errno 66] unknown>

    如果注释掉synchonrous函数 ,则报如下的错误 :


    Traceback (most recent call last):
    File "/usr/local/lib/python2.7/dist-packages/gevent-0.13.8-py2.7-linux-x86_64.egg/gevent/greenlet.py", line 390, in run
    result = self._run(*self.args, **self.kwargs)
    File "t3.py", line 9, in fetch
    response = urllib2.urlopen("http://www.baidu.com")
    File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
    File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
    File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
    File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
    File "/usr/lib/python2.7/urllib2.py", line 1207, in http_open
    return self.do_open(httplib.HTTPConnection, req)
    File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
    URLError: <urlopen error [Errno 66] unknown>
    <Greenlet at 0x1b02e10: fetch(9)> failed with URLError


    google了好久,完全没有找到解决方法啊,问别人,别人都说跑在他们的环境上没有问题,我也不知道我的环境有什么问题

    >>> import gevent
    >>> gevent.__version__
    '0.13.8'

    Best match: greenlet 0.4.1
    greenlet 0.4.1 is already the active version in easy-install.pth


    _(:з」∠)_ 到底为什么呀????

    v2ex 好歹是程序员的圈子,居然不能贴代码啊,这是什么逻辑?代码一上来全乱了
    2 条回复    1970-01-01 08:00:00 +08:00
    minggl
        1
    minggl  
       2013-06-26 20:45:53 +08:00
    1.gevent.monkey.patch_all(dns=false)试试
    2.换个gevent版本
    yeelone
        2
    yeelone  
    OP
       2013-06-28 14:06:33 +08:00
    @minggl 我把版本换到了 1.0dev 就没有报错了。ths
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1003 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 21:55 · PVG 05:55 · LAX 13:55 · JFK 16:55
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.