def readUrl(self,host,port,url,user,password):
error=False
tomcatUrl = "http://"+host+":"+str(port)+url
try:
pwdManager = urlconnection.HTTPPasswordMgrWithDefaultRealm()
pwdManager.add_password(None,tomcatUrl,user,password)
authHandler = urlconnection.HTTPBasicAuthHandler(pwdManager)
opener=urlconnection.build_opener(authHandler)
urlconnection.install_opener(opener)
req = urlconnection.Request(tomcatUrl)
handle = urlconnection.urlopen(req, None)
data = handle.read()
except HTTPError as e:
if(e.code==401):
data="ERROR: Unauthorized user. Does not have permissions. %s" %(e)
elif(e.code==403):
data="ERROR: Forbidden, yours credentials are not correct. %s" %(e)
else:
data="ERROR: The server couldn\'t fulfill the request. %s" %(e)
error=True
except URLError as e:
data = 'ERROR: We failed to reach a server. Reason: %s' %(e.reason)
error = True
except socket.timeout as e:
data = 'ERROR: Timeout error'
error = True
except socket.error as e:
data = "ERROR: Unable to connect with host "+self.host+":"+self.port
error = True
except:
data = "ERROR: Unexpected error: %s"%(sys.exc_info()[0])
error = True
return data,error
评论列表
文章目录