Jelajahi Sumber

make listen address and port configurable via commandline

milaq 7 tahun lalu
induk
melakukan
4432f1ef96
2 mengubah file dengan 12 tambahan dan 6 penghapusan
  1. 1 1
      README.md
  2. 11 5
      ycast.py

+ 1 - 1
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.

+ 11 - 5
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 = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
@@ -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: