Skip to content

Commit

Permalink
fix: Use netifaces instead of socket for get_local_ip (#100)
Browse files Browse the repository at this point in the history
* Use netifaces instead of socket for get_local_ip

* Update install requirements

..as we're now using netifaces and requests ourselves.
  • Loading branch information
theychx authored and skorokithakis committed May 15, 2018
1 parent e003fe7 commit 8e1cbc4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
2 changes: 1 addition & 1 deletion catt/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def setup_cast(device_name, video_url=None, prep=None, controller=None):

if video_url:
cc_info = (cast.device.manufacturer, cast.model_name)
stream = StreamInfo(video_url, model=cc_info, host=cast.host)
stream = StreamInfo(video_url, model=cc_info)

if controller:
if controller == "default":
Expand Down
13 changes: 6 additions & 7 deletions catt/stream_info.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import random
import socket
from pathlib import Path

import click
import netifaces
import youtube_dl

from .util import guess_mime
Expand All @@ -28,10 +28,10 @@ class CattInfoError(click.ClickException):


class StreamInfo:
def __init__(self, video_url, model=None, host=None):
def __init__(self, video_url, model=None):
if "://" not in video_url:
self._local_file = video_url
self.local_ip = self._get_local_ip(host)
self.local_ip = self._get_local_ip()
self.port = random.randrange(45000, 47000)
self.is_local_file = True
else:
Expand Down Expand Up @@ -160,10 +160,9 @@ def set_playlist_entry(self, number):
else:
self._active_entry = self._entries[number]

def _get_local_ip(self, cc_host):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.connect((cc_host, 0))
return sock.getsockname()[0]
def _get_local_ip(self):
interface = netifaces.gateways()['default'][netifaces.AF_INET][1]
return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']

def _get_stream_preinfo(self, video_url):
try:
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"youtube-dl>=2017.3.15",
"PyChromecast>=2.0.0",
"Click>=5.0",
"netifaces>=0.10.7",
"requests>=2.18.4",
]

test_requirements = [
Expand Down

0 comments on commit 8e1cbc4

Please sign in to comment.