Skip to content

Commit

Permalink
Improve error messages and argument parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
gareth-palmer committed Apr 23, 2021
1 parent 02c5ee7 commit 039e214
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cgiexecute
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def cgi_execute(hostname, timeout, urls, username, password):
raise ProgramError(error)

if response.headers['Content-Type'][0:8] != 'text/xml':
raise ProgramError('Unexpected Content-Type ' + response.headers['Content-Type'])
raise ProgramError('Unexpected Content-Type: ' + response.headers['Content-Type'])

try:
document = etree.fromstring(response.content)
Expand Down
22 changes: 13 additions & 9 deletions mediastream
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import getopt
import traceback
import socket
import ipaddress
import concurrent.futures
from lxml import etree
import requests
import requests.auth
import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject, GLib, Gst
import concurrent.futures


class ProgramError(Exception):
Expand All @@ -31,12 +31,16 @@ def cgi_execute(target_hostname, timeout, username, password, content):
else:
auth = None

response = requests.post(f'http://{target_hostname}:80/CGI/Execute',
timeout = timeout, auth = auth, data = {'XML': content})
response.raise_for_status()
try:
response = requests.post(f'http://{target_hostname}:80/CGI/Execute',
timeout = timeout, auth = auth, data = {'XML': content})
response.raise_for_status()

except requests.RequestException as error:
raise ProgramError(error)

if response.headers['Content-Type'][0:8] != 'text/xml':
raise ProgramError('Unexpected Content-Type ' + response.headers['Content-Type'])
raise ProgramError('Unexpected Content-Type: ' + response.headers['Content-Type'])

return response.content

Expand Down Expand Up @@ -344,11 +348,11 @@ def main():

target_hostnames = []

for argument in arguments:
if not re.search('(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', argument):
raise ProgramError(f'Invalid target host: {argument}')
for target_hostname in arguments:
if not re.search('(?xi) ^ (?: [a-z0-9\-]+ \.)* [a-z0-9\-]+ $', target_hostname):
raise ProgramError(f'Invalid target host: {target_hostname}')

target_hostnames.append(argument)
target_hostnames.append(target_hostname)

if wav_file is None:
raise ProgramError('No .wav file specified')
Expand Down

0 comments on commit 039e214

Please sign in to comment.