-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle timeout and misc. errors #19
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,19 +81,26 @@ def __init__(self, address, password, username='admin', protocol='http'): | |
self.password = password | ||
|
||
def post(self, payload, ns=None): | ||
resp = requests.post(self.uri, | ||
headers={'content-type': | ||
'application/soap+xml;charset=UTF-8'}, | ||
auth=HTTPDigestAuth(self.username, self.password), | ||
data=payload) | ||
resp.raise_for_status() | ||
if ns: | ||
rv = _return_value(resp.content, ns) | ||
if rv == 0: | ||
return 0 | ||
print(pp_xml(resp.content)) | ||
else: | ||
return 0 | ||
try: | ||
resp = requests.post(self.uri, | ||
headers={'content-type': | ||
'application/soap+xml;charset=UTF-8'}, | ||
auth=HTTPDigestAuth(self.username, self.password), | ||
data=payload, | ||
timeout=5.0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The request timeout is typically just a backstop, and I'd be concerned that 5 seconds might not be enough, How about we make it 30? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree, 5 seconds is too short. It would be nice to make this a command line arg, because it can vary widely depending on your situation |
||
resp.raise_for_status() | ||
except requests.exceptions.Timeout: | ||
print "Request timed out." | ||
except requests.exceptions.RequestException as e: | ||
print e | ||
else: | ||
if ns: | ||
rv = _return_value(resp.content, ns) | ||
if rv == 0: | ||
return 0 | ||
print(pp_xml(resp.content)) | ||
else: | ||
return 0 | ||
|
||
def power_on(self): | ||
"""Power on the box.""" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd rather not do the try here. If this is consumed as a library, it makes sense for these exceptions to flow up to the next layer so that they can handle these errors however they like.
Instead I would put the exception handling and error messages printed to the user here -
amt/bin/amtctrl
Line 115 in d3e5d74
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, that looks good.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another error I'd like to catch is "KeyboardInterrupt". If I control-C while looping calls to amtctrl, the stack trace dump makes a mess out of my terminal screen. Thanks.