diff --git a/README.md b/README.md index 2251cb0..c2ea2e0 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ You can also have a look at the provided [example](stations.yml.example) to bett While you can simply run YCast with root permissions listening on all interfaces on port 80, this may not be desired for various reasons. -You can (and should) change the `listen_port` and `listen_address` variables in `ycast.py` if you are already running a HTTP server on the target machine +You can (and should) change the listen address and port (via `-l` and `-p` respectively) if you are already running a HTTP server on the target machine and/or want to proxy or restrict YCast access. It is advised to use a proper webserver (e.g. Nginx) in front of YCast if you can. diff --git a/ycast.py b/ycast.py index a865849..a39e7c3 100755 --- a/ycast.py +++ b/ycast.py @@ -1,14 +1,12 @@ #!/usr/bin/env python3 import os +import argparse from http.server import BaseHTTPRequestHandler, HTTPServer import xml.etree.cElementTree as etree import yaml -listen_address = '0.0.0.0' -listen_port = 80 - VTUNER_DNS = 'http://radioyamaha.vtuner.com' VTUNER_INITURL = '/setupapp/Yamaha/asp/BrowseXML/loginXML.asp' XMLHEADER = '' @@ -83,8 +81,16 @@ class YCastServer(BaseHTTPRequestHandler): get_stations() -server = HTTPServer((listen_address, listen_port), YCastServer) -print('YCast server listening on %s:%s' % (listen_address, listen_port)) +parser = argparse.ArgumentParser(description='vTuner API emulation') +parser.add_argument('-l', action='store', dest='address', help='Listen address', default='0.0.0.0') +parser.add_argument('-p', action='store', dest='port', type=int, help='Listen port', default=80) +arguments = parser.parse_args() +try: + server = HTTPServer((arguments.address, arguments.port), YCastServer) +except PermissionError: + print("Error: No permission to create socket. Are you trying to use ports below 1024 without elevated rights?") + raise +print('YCast server listening on %s:%s' % (arguments.address, arguments.port)) try: server.serve_forever() except KeyboardInterrupt: