I want to make a program which is checks the given url for every 10 seconds
def listener(url):
print ("Status: Listening")
response = urllib.request.urlopen(url)
data = response.read()
text = data.decode('utf-8')
if text == 'Hello World. Testing!':
print("--Action Started!--")
t = Timer(10.0,listener('http://test.com/test.txt'))
t.start()
here is the output:
Status: Listening
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.4/threading.py", line 920, in _bootstrap_inner
self.run()
File "/usr/lib/python3.4/threading.py", line 1186, in run
self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable
It runs the function for one time, after 10 seconds the error appears
Currently, the result of your listener
function call is being given to Timer
, which is resultantly None
, as you need to make an explicit return so your Timer
has something to operate on.
You have to place the argument for your listener()
in another parameter, like this. Note that args
must be a sequence, thus the tuple with the comma placed after your url
. Without this tuple, you are passing each individual character from 'http://test.com/test.txt'
as an argument to listener
.
t = Timer(10.0,listener,args=('http://test.com/test.txt',))
As you can see in documentation here, arguments must be passed as a third parameter.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments