aboutsummaryrefslogtreecommitdiff
path: root/youtube_dl/compat.py
diff options
context:
space:
mode:
authorPhilipp Hagemeister <phihag@phihag.de>2015-01-10 19:55:36 +0100
committerPhilipp Hagemeister <phihag@phihag.de>2015-01-10 19:56:51 +0100
commitbe4a824d74add1a3b78b8244dff12f4f078f168a (patch)
treef1dd3bb5d8625bdeec73a0ba62f6074173f6618b /youtube_dl/compat.py
parent6ce08764a1faba3eeecbd2a04643f3266c8c5111 (diff)
Add new option --source-address
Closes #3618, fixes #721, fixes #2481, fixes #4551, closes #1020.
Diffstat (limited to 'youtube_dl/compat.py')
-rw-r--r--youtube_dl/compat.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py
index 46d438846..44a902573 100644
--- a/youtube_dl/compat.py
+++ b/youtube_dl/compat.py
@@ -4,6 +4,7 @@ import getpass
import optparse
import os
import re
+import socket
import subprocess
import sys
@@ -307,6 +308,32 @@ else:
compat_kwargs = lambda kwargs: kwargs
+if sys.version_info < (2, 7):
+ def compat_socket_create_connection(address, timeout, source_address=None):
+ host, port = address
+ err = None
+ for res in socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM):
+ af, socktype, proto, canonname, sa = res
+ sock = None
+ try:
+ sock = socket.socket(af, socktype, proto)
+ sock.settimeout(timeout)
+ if source_address:
+ sock.bind(source_address)
+ sock.connect(sa)
+ return sock
+ except socket.error as _:
+ err = _
+ if sock is not None:
+ sock.close()
+ if err is not None:
+ raise err
+ else:
+ raise error("getaddrinfo returns an empty list")
+else:
+ compat_socket_create_connection = socket.create_connection
+
+
# Fix https://github.com/rg3/youtube-dl/issues/4223
# See http://bugs.python.org/issue9161 for what is broken
def workaround_optparse_bug9161():
@@ -343,6 +370,7 @@ __all__ = [
'compat_parse_qs',
'compat_print',
'compat_str',
+ 'compat_socket_create_connection',
'compat_subprocess_get_DEVNULL',
'compat_urllib_error',
'compat_urllib_parse',