Skip to content
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

Fix some exceptions. #18

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions upnpy/ssdp/SSDPDevice.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def _get_services_request(self):
service_string = service.getElementsByTagName('serviceType')[0].firstChild.nodeValue
service_id = service.getElementsByTagName('serviceId')[0].firstChild.nodeValue
scpd_url = service.getElementsByTagName('SCPDURL')[0].firstChild.nodeValue
control_url = service.getElementsByTagName('controlURL')[0].firstChild.nodeValue
event_sub_url = service.getElementsByTagName('eventSubURL')[0].firstChild.nodeValue
control_url = utils.safe_get_firstChild(service, 'controlURL')
event_sub_url = utils.safe_get_firstChild(service, 'eventSubURL')

parsed_service_id = utils.parse_service_id(service_id)

Expand Down Expand Up @@ -317,7 +317,10 @@ def _get_description_request(self):

try:
service_description = utils.make_http_request(self.scpd_url).read()
self.description = service_description.decode()
if not service_description:
self.description = exceptions.NotAvailableError
else:
self.description = service_description.decode()
except urllib.error.HTTPError as e:
if e.code == 404:
self.description = exceptions.NotAvailableError
Expand Down
9 changes: 9 additions & 0 deletions upnpy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,12 @@ def make_http_request(url, data=None, headers=None):
# If data is provided the request method will automatically be set to POST by urllib
request = urllib.request.Request(url, data=data, headers=headers)
return urllib.request.urlopen(request)


def safe_get_firstChild(doc, element_name):
node = doc.getElementsByTagName(element_name)[0].firstChild

try:
return node.nodeValue
except AttributeError:
pass