Skip to content

Commit

Permalink
Provide SSL context field
Browse files Browse the repository at this point in the history
The allows the caller to provide proper SSL parameters and avoid dirty monkey patching to
suppress SSL errors.
  • Loading branch information
Chocobo1 committed Dec 26, 2024
1 parent 90e457a commit cc31a90
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/searchengine/nova3/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import re
import socket
import socks
import ssl
import sys
import tempfile
import urllib.error
Expand Down Expand Up @@ -76,12 +77,12 @@ def getBrowserUserAgent() -> str:
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 """

request = urllib.request.Request(url, request_data, {**headers, **custom_headers})
try:
response = urllib.request.urlopen(request)
response = urllib.request.urlopen(request, context=ssl_context)
except urllib.error.URLError as errno:
print(f"Connection error: {errno.reason}", file=sys.stderr)
return ""
Expand All @@ -104,14 +105,14 @@ def retrieve_url(url: str, custom_headers: Mapping[str, Any] = {}, request_data:
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 url
request = urllib.request.Request(url, headers=headers)
if referer is not None:
request.add_header('referer', referer)
response = urllib.request.urlopen(request)
response = urllib.request.urlopen(request, context=ssl_context)
data = response.read()

# Check if it is gzipped
Expand Down

0 comments on commit cc31a90

Please sign in to comment.