推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide
delavior
V2EX  ›  Python

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

  •  
  •   delavior ·
    delavior · May 13, 2015 · 5370 views
    This topic created in 4192 days ago, the information mentioned may be changed or developed.

    这是我从网上找的一个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下的执行结果:
    [[email protected] 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

    Supplement 1  ·  May 14, 2015
    谢谢大家的回复,增加了错误信息,输出如下:
    fail--code: 502 ,msg: Server dropped connection ,reason: Server dropped connecti
    on
    fail--code: 502 ,msg: Server dropped connection ,reason: Server dropped connecti
    on

    还是不懂为啥会502,ping是能ping通的,而且apache服务肯定是启动的,要不然linux下(就是所请求的那台机器)自己执行也不会成功吧
    21 replies  •  2015-05-15 08:39:49 +08:00
    ivanlw
    ivanlw
    May 13, 2015
    1
    断点逐句调试
    linkiosk
    linkiosk
    May 13, 2015
    2
    在编辑器中转一下换行方式,转为windows的
    laoyur
    laoyur
    May 13, 2015
    3
    喜闻乐见py格式被毁,别人想帮你测试都没门了
    Livid
    Livid
    MOD
    PRO
    May 13, 2015❤️1
    4
    @laoyur 我帮楼主更新了一下帖子格式。

    V2EX 是支持 Markdown 插入代码的。
    realityone
    realityone
    May 13, 2015
    5
    except urllib2.URLError as err:
    print 'We failed,but I don\'t know the reason'
    我不太建议写成这样。。好歹输出点有用的信息让人知道什么异常
    saber000
    saber000
    May 13, 2015
    6
    Python版本不一样吧?
    刚好我有个ZabbixApi的实现:https://github.com/MrLYC/ZabbixPythonApi
    使用pip install zabbixapi就能安装上.
    使用方式:
    from zabbixapi.zabbixapi import ZabbixAPI

    zapi = ZabbixAPI("http://192.168.0.244/zabbix/api_jsonrpc.php")
    zapi.login("Admin", "zabbix")
    result = zapi.host.get({"output": ["hostid", "host"], "selectInterfaces": ["interfaceid", "ip"]})
    print result

    这样就相当于你上面的代码了
    binux
    binux
    May 13, 2015
    7
    You failed,but we don't know the reaseon
    jugelizi
    jugelizi
    May 13, 2015
    8
    大快人心
    曾经也是win下跑不出结果
    换到linux啥问题也没有
    ericls
    ericls
    May 13, 2015 via Android
    9
    是同一个 interpreter 么?
    Septembers
    Septembers
    May 13, 2015
    10
    @Livid 建议做一个主题发布/回复指南统一说明V2EX支持那些语法,进而提升主题/回复质量
    wind3110991
    wind3110991
    May 14, 2015 via iPhone
    11
    能用下markdown吗。。。py首行缩进没了感觉在这个社区还是不太合适吧。。毕竟一群pythoner
    delavior
    delavior
    OP
    May 14, 2015
    12
    @realityone 嗯嗯,加了code和msg,输出是502
    delavior
    delavior
    OP
    May 14, 2015
    13
    @saber000 谢谢
    neutrino
    neutrino
    May 14, 2015
    14
    @binux You have been weighed and found wanting.
    r00tt
    r00tt
    May 14, 2015
    15
    @laoyur 貌似支持gist的
    WKPlus
    WKPlus
    May 14, 2015
    16
    @Septembers @Livid 正在摸索发帖时支持哪些格式,非常赞同这个提议:建议做一个主题发布/回复指南统一说明V2EX支持那些语法,进而提升主题/回复质量
    endrollex
    endrollex
    May 14, 2015
    17
    @Septembers 同感,找不到帮助,Markdown用的少背不出
    WKPlus
    WKPlus
    May 14, 2015
    18
    可以先在windows机器上用浏览器插件来调用zabbix api,看看是否OK,这样可以用来排查是网络问题还是python(解析器版本或者其他什么)的问题
    CRVV
    CRVV
    May 14, 2015
    19
    URLError没改成urllib2.URLError的时候在Linux下能执行成功?
    我对这个表示怀疑

    另外像楼上说的,应该先排除网络的原因
    xzem
    xzem
    May 14, 2015 via Android
    20
    版本问题?
    mskip
    mskip
    May 15, 2015
    21
    @binux 哈哈,去大大的博客取经去了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1444 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 79ms · UTC 22:33 · PVG 06:33 · LAX 14:33 · JFK 17:33
    ♥ Do have faith in what you're doing.