So this is a script that I am coding for my buddies companies customer support. Basically, what it does is call using the IP phones that are in the script, it works, but with problems. Here is the code:
import urllib, urllib2, sys
num = sys.argv[1]
print 'Calling'
phones = [
'http://phone1/index.htm',
'http://phone2/index.htm',
'https://phone3/index.htm',
'https://phone4/index.htm',
'https://phone5/index.htm'
]
data = urllib.urlencode({"NUMBER":num, "DIAL":"Dial", "active_line":1})
while 1:
for phone in phones:
try:
urllib2.urlopen(phone,data) # make call
urllib2.urlopen(phone+"?dialeddel=0") # clear logs
except: pass
The first problem is that it only calls using phone one... Right now it is setup to keep calling over and over, for debugging purposes, and I seem to only be getting calls from phone one... The second problem is that the script will not terminate. ctrl+c does not work... The only way to terminate it (that I know of) is to end the ssh session. Now, I am new to python so both of these are probably just stupid mistakes so hopefully someone can help. Thanks!
3 Answers 3
Try replacing the try catch block like this,
try:
<code>
except Exception as exp:
<code>
While using the below code.
except:
pass
The system will catch SystemExit, KeyboardInterrupt and other things that you probably don't want to catch.
Thanks.
1 Comment
Your try block may be catching the Ctrl-C. Try modifying it so you only catch the specific exceptions you expect. See KeyboardInterrupt for more about Ctrl-C as an exception.
Comments
Replace:
try:
urllib2.urlopen(phone,data) # make call
urllib2.urlopen(phone+"?dialeddel=0") # clear logs
except: pass
with:
urllib2.urlopen(phone,data) # make call
urllib2.urlopen(phone+"?dialeddel=0") # clear logs
so you can see traceback and find the error
except: passand see if it's generating any errors.ctrl + zto terminate. If it does nothig, you can open another ssh session and perfomkillall pythonKeyboardInterrupt-- such as is raised by hitting ctrl-c -- is an exception too, andexcept: passsays that if any exception occurs, ignore it. If you hold down control-C for long enough, you'll luck out and break it eventually.