mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-07 08:02:30 -06:00
Provide SSL context field
The allows the caller to provide proper SSL parameters and avoid dirty monkey patching to suppress SSL errors.
This commit is contained in:
@@ -35,6 +35,7 @@ import os
|
|||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
import socks
|
import socks
|
||||||
|
import ssl
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
import tempfile
|
||||||
import urllib.error
|
import urllib.error
|
||||||
@@ -76,12 +77,12 @@ if "sock_proxy" in os.environ and len(os.environ["sock_proxy"].strip()) > 0:
|
|||||||
htmlentitydecode = html.unescape
|
htmlentitydecode = html.unescape
|
||||||
|
|
||||||
|
|
||||||
def retrieve_url(url: str, custom_headers: Mapping[str, Any] = {}, request_data: Optional[Any] = None) -> str:
|
def retrieve_url(url: str, custom_headers: Mapping[str, Any] = {}, request_data: Optional[Any] = None, ssl_context: Optional[ssl.SSLContext] = None) -> str:
|
||||||
""" Return the content of the url page as a string """
|
""" Return the content of the url page as a string """
|
||||||
|
|
||||||
request = urllib.request.Request(url, request_data, {**headers, **custom_headers})
|
request = urllib.request.Request(url, request_data, {**headers, **custom_headers})
|
||||||
try:
|
try:
|
||||||
response = urllib.request.urlopen(request)
|
response = urllib.request.urlopen(request, context=ssl_context)
|
||||||
except urllib.error.URLError as errno:
|
except urllib.error.URLError as errno:
|
||||||
print(f"Connection error: {errno.reason}", file=sys.stderr)
|
print(f"Connection error: {errno.reason}", file=sys.stderr)
|
||||||
return ""
|
return ""
|
||||||
@@ -104,14 +105,14 @@ def retrieve_url(url: str, custom_headers: Mapping[str, Any] = {}, request_data:
|
|||||||
return dataStr
|
return dataStr
|
||||||
|
|
||||||
|
|
||||||
def download_file(url: str, referer: Optional[str] = None) -> str:
|
def download_file(url: str, referer: Optional[str] = None, ssl_context: Optional[ssl.SSLContext] = None) -> str:
|
||||||
""" Download file at url and write it to a file, return the path to the file and the url """
|
""" Download file at url and write it to a file, return the path to the file and the url """
|
||||||
|
|
||||||
# Download url
|
# Download url
|
||||||
request = urllib.request.Request(url, headers=headers)
|
request = urllib.request.Request(url, headers=headers)
|
||||||
if referer is not None:
|
if referer is not None:
|
||||||
request.add_header('referer', referer)
|
request.add_header('referer', referer)
|
||||||
response = urllib.request.urlopen(request)
|
response = urllib.request.urlopen(request, context=ssl_context)
|
||||||
data = response.read()
|
data = response.read()
|
||||||
|
|
||||||
# Check if it is gzipped
|
# Check if it is gzipped
|
||||||
|
|||||||
Reference in New Issue
Block a user