美文网首页
python3+urllib ping通服务器

python3+urllib ping通服务器

作者: abrila | 来源:发表于2018-12-25 14:22 被阅读0次

    使用urllib ping通服务器

    # -*- coding: utf-8 -*-
    import urllib.request
    import urllib.error
    import sys
    import json
    import base64
    import M2Crypto
    def ping(urlbase,param = None):
        url  = urlbase+"/v1/ping"
        status , response = http_post(url)
        if status:
            print("server ok!")
        else:
            print("server failed!")
    def http_post(url,data=""):
        try:
            f = urllib.parse.urlencode(data)
            f = f.encode('utf-8')
            req = urllib.request.Request(url,f)
            response = urllib.request.urlopen(req)
        except urllib.error.HTTPError as e:
            print("HTTPerror = "+str(e.code))
        except urllib.error.URLError as e:
            print('URLError = ' + str(e.reason))
        except httplib.HTTPException as e:
            print('HTTPException')
        except Exception:
            import traceback
            print('generic exception: ' + traceback.format_exc())
        else:
            content = response.read()
            response.close()
            return True, content
        return False, None
    if __name__ == "__main__":
        ping("http://×××.×××.×××.×××:×××")
    

    相关文章

      网友评论

          本文标题:python3+urllib ping通服务器

          本文链接:https://www.haomeiwen.com/subject/qmrflqtx.html