make listen address and port configurable via commandline

This commit is contained in:
milaq 2018-07-23 18:27:15 +02:00
parent e28347a1a4
commit 4432f1ef96
2 changed files with 12 additions and 6 deletions

View file

@ -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.

View file

@ -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: