Search code examples
pythonhttprequestreal-timeurllib

'maximum recursion depth exceeded' when loop urllib


Helo, i have problem. I'm new in python. I try learn httprequest with urllib for get realtime data from site. So i try to loop with minim delay. And have error :

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

Code

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

Error for this Code

RuntimeError: maximum recursion depth exceeded in cmp

Maybe you have solution for fix this code, or you have another way for realtime get data from site.

Sorry for my bad english.

Thank you


Solution

  • test calls open which call test ; cause infinite recursion.

    Use loop instead to prevent such recursion:

    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 is a builtin function. By defining function as open, it shadows the builtin function. Use other name.