'超出最大递归深度'当循环urllib

时间:2016-03-22 05:31:13

标签: python httprequest real-time urllib

Helo,我有问题。我是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

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

抱歉我的英语不好。

谢谢

1 个答案:

答案 0 :(得分:2)

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()

BTW,open是内置函数。通过将函数定义为open,它会隐藏内置函数。使用其他名称。