瀏覽代碼

make listen address and port configurable via commandline

milaq 7 年之前
父節點
當前提交
4432f1ef96
共有 2 個文件被更改,包括 12 次插入6 次删除
  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.
 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.
 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.
 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
 #!/usr/bin/env python3
 
 
 import os
 import os
+import argparse
 from http.server import BaseHTTPRequestHandler, HTTPServer
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import xml.etree.cElementTree as etree
 import xml.etree.cElementTree as etree
 
 
 import yaml
 import yaml
 
 
-listen_address = '0.0.0.0'
-listen_port = 80
-
 VTUNER_DNS = 'http://radioyamaha.vtuner.com'
 VTUNER_DNS = 'http://radioyamaha.vtuner.com'
 VTUNER_INITURL = '/setupapp/Yamaha/asp/BrowseXML/loginXML.asp'
 VTUNER_INITURL = '/setupapp/Yamaha/asp/BrowseXML/loginXML.asp'
 XMLHEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
 XMLHEADER = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'
@@ -83,8 +81,16 @@ class YCastServer(BaseHTTPRequestHandler):
 
 
 
 
 get_stations()
 get_stations()
-server = HTTPServer((listen_address, listen_port), YCastServer)
+parser = argparse.ArgumentParser(description='vTuner API emulation')
-print('YCast server listening on %s:%s' % (listen_address, listen_port))
+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:
 try:
     server.serve_forever()
     server.serve_forever()
 except KeyboardInterrupt:
 except KeyboardInterrupt: