0

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!

6
  • 6
    Step one is to take out that except: pass and see if it's generating any errors. Commented Sep 1, 2012 at 1:56
  • Try to use ctrl + z to terminate. If it does nothig, you can open another ssh session and perfom killall python Commented Sep 1, 2012 at 2:03
  • "The second problem is that the script will not terminate. ctrl+c does not work..." You explicitly told it not to let you control-C it. KeyboardInterrupt -- such as is raised by hitting ctrl-c -- is an exception too, and except: pass says that if any exception occurs, ignore it. If you hold down control-C for long enough, you'll luck out and break it eventually. Commented Sep 1, 2012 at 2:04
  • Here is the error it gets, File "p.py", line 16 ^ SyntaxError: invalid syntax Commented Sep 1, 2012 at 2:04
  • @DSM I didn't know that terminating the script was an exception. Commented Sep 1, 2012 at 2:05

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That seems to yield the same result.
0

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

0

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

1 Comment

When doing that, it will call just once and output Calling... but then it shows this... pastebin.com/vnHyHFnR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.