使用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://×××.×××.×××.×××:×××")
网友评论