'maximum recursion depth exceeded' 当循环 urllib

'maximum recursion depth exceeded' when loop urllib

你好,我有问题。我是 python 的新人。我尝试使用 urllib 学习 httprequest 以从站点获取实时数据。所以我尝试以最小延迟循环。并且有错误:

Exception RuntimeError: 'maximum recursion depth exceeded' in <bound method _fileobject.__del__ of <socket._fileobject object at 0x01D650F0>> ignored

代码

import urllib
import time
import os
def open(url):
    r = urllib.urlopen(url)
    #print(r.read())
    html = r.read()
    if(html!='-'):
        print('ok')
        time.sleep(0.1)
        os.system('cls' if os.name=='nt' else 'clear')
        test()

def test():
    open('http://127.0.0.1/test/')

test()

此代码错误

RuntimeError: maximum recursion depth exceeded in cmp

也许您有修复此代码的解决方案,或者您有另一种实时从站点获取数据的方法。

抱歉我的英语不好。

谢谢

test 调用 open 调用 test ;导致无限递归。

使用循环来防止这种递归:

import urllib
import time
import os

def check(url):
    r = urllib.urlopen(url)
    html = r.read()
    if html != '-':
        print('ok')
        time.sleep(0.1)
        os.system('cls' if os.name == 'nt' else 'clear')

def test():
    while True:
        check('http://127.0.0.1/test/')

test()

顺便说一句,open 是一个内置函数。通过将函数定义为 open,它隐藏了内置函数。使用其他名称。