X-Git-Url: http://git.cielonegro.org/gitweb.cgi?a=blobdiff_plain;f=youtube_dl%2Fcompat.py;h=497ca52de14e59aabbc5587a0c6238e02dd37a48;hb=52585fd6dcd5020f2800d133588d7232419dddc0;hp=46d438846787e9a95ea8631c869f96341d4ccb9c;hpb=c24dfef63c55ef1a5424d11b485c3b76245448a4;p=youtube-dl.git diff --git a/youtube_dl/compat.py b/youtube_dl/compat.py index 46d438846..497ca52de 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 @@ -70,6 +71,11 @@ try: except ImportError: compat_subprocess_get_DEVNULL = lambda: open(os.path.devnull, 'w') +try: + import http.server as compat_http_server +except ImportError: + import BaseHTTPServer as compat_http_server + try: from urllib.parse import unquote as compat_urllib_parse_unquote except ImportError: @@ -307,6 +313,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 socket.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(): @@ -338,10 +370,12 @@ __all__ = [ 'compat_html_entities', 'compat_html_parser', 'compat_http_client', + 'compat_http_server', 'compat_kwargs', 'compat_ord', 'compat_parse_qs', 'compat_print', + 'compat_socket_create_connection', 'compat_str', 'compat_subprocess_get_DEVNULL', 'compat_urllib_error',