0

所以这是我为我的伙伴公司客户支持编写的脚本。基本上,它所做的是使用脚本中的 IP 电话进行呼叫,它可以工作,但有问题。这是代码:

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

第一个问题是它只使用电话一...现在它被设置为不断调用,出于调试目的,我似乎只接到电话一...第二个问题是脚本不会终止。ctrl+c 不起作用...终止它的唯一方法(据我所知)是结束 ssh 会话。现在,我是 python 新手,所以这两个可能只是愚蠢的错误,所以希望有人能提供帮助。谢谢!

4

3 回答 3

1

尝试像这样替换 try catch 块,

try:
    <code>
except Exception as exp:
    <code>

使用以下代码时。

except:
    pass

系统将捕获 SystemExit、KeyboardInterrupt 和其他您可能不想捕获的东西。

谢谢。

于 2012-09-01T02:05:50.150 回答
0

您的 try 块可能正在捕获 Ctrl-C。尝试修改它,以便只捕获您期望的特定异常。有关 Ctrl-C 作为例外的更多信息,请参阅KeyboardInterrupt 。

于 2012-09-01T02:09:34.480 回答
0

代替:

try:
    urllib2.urlopen(phone,data) # make call
    urllib2.urlopen(phone+"?dialeddel=0") # clear logs
except: pass

和:

urllib2.urlopen(phone,data) # make call
urllib2.urlopen(phone+"?dialeddel=0") # clear logs

所以你可以看到回溯并找到错误

于 2012-09-01T02:13:52.917 回答