求助,同一个 py 文件在 linux 和 windows 下执行结果不一样

2015-05-13 13:38:57 +08:00
 delavior

这是我从网上找的一个python post访问zabbix接口的demo

import json
import urllib2

zabbix_url="http://192.168.0.244/zabbix/api_jsonrpc.php"
zabbix_header={"Content-Type":"application/json"}
auth_data=json.dumps({"jsonrpc":"2.0","method":"user.login","params":{"user":"Admin","password":"zabbix"},"id":0})
auth_code=""

request=urllib2.Request(zabbix_url,auth_data)
for key in zabbix_header:
    request.add_header(key,zabbix_header[key])
try:
    result=urllib2.urlopen(request)
except urllib2.URLError as err:
    print 'We failed,but I don\'t know the reaseon'
else:
    response=json.loads(result.read())
    result.close()
    if 'result' in response:
        auth_code=response['result']
        print 'We succeed,and the auth_id is ',auth_code
    else:
        print response['error']['data']

hosts_data=json.dumps({"jsonrpc":"2.0","method":"host.get","params":{"output":["hostid","host"],"selectInterfaces":["interfaceid","ip"]},"id":1,"auth":auth_code})

request=urllib2.Request(zabbix_url,hosts_data)
for key in zabbix_header:
    request.add_header(key,zabbix_header[key])
try:
    result=urllib2.urlopen(request)
except urllib2.URLError as e:
    print 'fail'
else:
    response=json.loads(result.read())
    result.close()
    for host in response['result']:
        print "Host ID:",host['hostid'],",Host Name:",host['host'],",Interface ID:",host['interfaces'][0]['interfaceid'],",ip:",host['interfaces'][0]['ip']
    print "Number of hosts:",len(response['result'])

在linux下的执行结果:
[root@192.168.0.244 PyDir]# python zabbix_api_demo.py
We succeed,and the auth_id is d9367fded69993832fd64f60d8375a03
Host ID: 10084 ,Host Name: Zabbix server ,Interface ID: 1 ,ip: 127.0.0.1
Host ID: 10132 ,Host Name: jmxDemo ,Interface ID: 29 ,ip: 127.0.0.1
Host ID: 10133 ,Host Name: SnmpDemo ,Interface ID: 30 ,ip: 127.0.0.1
Host ID: 10136 ,Host Name: ligang ,Interface ID: 33 ,ip: 192.168.0.28
Host ID: 10137 ,Host Name: yegucheng ,Interface ID: 34 ,ip: 192.168.0.129
Host ID: 10155 ,Host Name: 192.168.0.233 ,Interface ID: 53 ,ip: 192.168.0.233
Host ID: 10174 ,Host Name: delavior ,Interface ID: 68 ,ip: 192.168.0.147
Host ID: 10175 ,Host Name: UbuntuDemo ,Interface ID: 69 ,ip: 192.168.0.20
Host ID: 10176 ,Host Name: DemoHost ,Interface ID: 72 ,ip: 192.168.0.20
Host ID: 10177 ,Host Name: cxj demo ,Interface ID: 73 ,ip: 192.168.0.139
Number of hosts: 10

在windows下的执行结果,开始是这个:
G:\Desktop>py -2 zabbix_api_demo.py
Traceback (most recent call last):
File "zabbix_api_demo.py", line 14, in <module>
except URLError as err:
NameError: name 'URLError' is not defined

然后我把URLError改成了urllib2.URLError,然后结果变成了这个:
G:\Desktop>py -2 zabbix_api_demo.py
We failed,but I don't know the reaseon
fail

不懂python,不太明白为啥同样的文件在linux下和windows下执行结果不一样,麻烦给指点一下

ps:linux下的python版本是2.7.9,windows下装了两个,一个是2.7.9,一个是3.4.3

4409 次点击
所在节点    Python
21 条回复
mskip
2015-05-15 08:39:49 +08:00
@binux 哈哈,去大大的博客取经去了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/190725

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX