1- """Get a request response for https only with curl"""
1+ """Get a request response for https only with curl. """
22import os
33import sys
44import time
@@ -38,8 +38,9 @@ class AcquireLock:
3838 lock (socket.socket): The TCP socket used to enforce the single instance.
3939 To protect the server from user error.
4040 """
41+
4142 def __init__ (self ):
42- # Create a TCP socket
43+ """Set the lock, so that it don't accidentally span too many requests."""
4344 self .lock = socket .socket (socket .AF_INET , socket .SOCK_STREAM )
4445 try :
4546 # Bind to localhost and a specific port
@@ -63,13 +64,15 @@ class RequestHandler:
6364 """Handle HTTPS requests with retry and certificate handling."""
6465
6566 def __init__ (self ):
67+ """Set default values."""
6668 self .misconfigured_server = False
6769 self .leaf_cert = None
6870
6971 def validate_url (self , url ):
7072 """Limit to used domains."""
7173 allowed_domains = (
7274 "https://www.trle.net/" ,
75+ "https://trle.net/" ,
7376 "https://trcustoms.org/" ,
7477 "https://data.trcustoms.org/"
7578 )
@@ -127,7 +130,7 @@ def validate_data_type(self, content_type):
127130 sys .exit (1 )
128131
129132 def set_leaf (self , curl ):
130- """Write the certificate to a temporary file manually"""
133+ """Write the certificate to a temporary file manually. """
131134 if self .leaf_cert is None :
132135 raise ValueError ("Leaf certificate is None and cannot be written to a file." )
133136
@@ -142,7 +145,7 @@ def set_leaf(self, curl):
142145 return temp_cert_path
143146
144147 def get_leaf (self , url ):
145- """Check if we need to grab the first certificate"""
148+ """Check if we need to grab the first certificate. """
146149 if not self .misconfigured_server :
147150 self .leaf_cert = get_leaf_cert .run (url )
148151 if not isinstance (self .leaf_cert , bytes ):
@@ -156,15 +159,15 @@ def get_leaf(self, url):
156159 sys .exit (1 )
157160
158161 def setup_before_get_response (self , url , content_type ):
159- """validate known url and content type"""
162+ """Validate known URL and content type. """
160163 self .validate_url (url )
161164 self .validate_data_type (content_type )
162165
163166 if url .startswith ("https://www.trle.net/" ) and not self .misconfigured_server :
164167 self .get_leaf (url )
165168
166169 def get_response (self , url , content_type ):
167- """Handle all https requests"""
170+ """Handle all https requests. """
168171 self .setup_before_get_response (url , content_type )
169172
170173 if content_type == 'application/zip' :
@@ -228,7 +231,7 @@ def get_response(self, url, content_type):
228231 return self .close_response (curl , headers , response_buffer , content_type )
229232
230233 def close_response (self , curl , headers , response_buffer , content_type ):
231- """Pack response and close curl"""
234+ """Pack response and close curl. """
232235 if curl is None :
233236 logging .error ("No curl instance" )
234237 sys .exit (1 )
@@ -252,7 +255,7 @@ def close_response(self, curl, headers, response_buffer, content_type):
252255 sys .exit (1 )
253256
254257 def pack_response_buffer (self , content_type , response_buffer ):
255- """Validate and return the response based on content type"""
258+ """Validate and return the response based on content type. """
256259 if content_type == 'text/html' :
257260 raw_data = response_buffer .getvalue ()
258261 for encoding in ['utf-8' , 'windows-1252' , 'utf-16' , 'utf-32' ]:
@@ -272,8 +275,7 @@ def pack_response_buffer(self, content_type, response_buffer):
272275 return None
273276
274277 def extract_content_type (self , headers ):
275- """Read the header lines to look for content-type"""
276-
278+ """Read the header lines to look for content-type."""
277279 for header in headers .splitlines ():
278280 if header .lower ().startswith ('content-type:' ):
279281 return header .split (':' , 1 )[1 ].split (';' )[0 ].strip ()
@@ -282,19 +284,21 @@ def extract_content_type(self, headers):
282284
283285
284286class Downloader :
285- """Zip file downloader to be used in RequestHandler"""
287+ """Zip file downloader to be used in RequestHandler."""
288+
286289 def __init__ (self ):
290+ """Set default values."""
287291 self .buffer = BytesIO ()
288292 self .status = 0
289293 self .progress_bar = None
290294
291295 def write_callback (self , data ):
292- """Callback function for writing downloaded data."""
296+ """Write the downloaded data."""
293297 self .buffer .write (data )
294298 return len (data )
295299
296300 def progress_callback (self , total_to_download , downloaded , total_to_upload , uploaded ):
297- """Callback function for reporting download progress.
301+ """Report download progress.
298302
299303 Args:
300304 total_to_download (int): Total size of the file to download.
@@ -319,7 +323,7 @@ def progress_callback(self, total_to_download, downloaded, total_to_upload, uplo
319323
320324 def download_file (self , url ):
321325 """
322- Downloads a file from the specified URL and stores its contents in a buffer.
326+ Download a file from the specified URL and stores its contents in a buffer.
323327
324328 This method utilizes the `pycurl` library to perform the download, providing
325329 a progress bar for user feedback. It handles server misconfigurations,
@@ -434,7 +438,7 @@ def download_file(self, url):
434438
435439def get (url , content_type ):
436440 """
437- Get server response from TRLE or Trcustom hosts
441+ Get server response from TRLE or Trcustom hosts.
438442
439443 content_type:
440444 'application/json'
@@ -454,10 +458,10 @@ def get(url, content_type):
454458
455459
456460def release_lock ():
457- """Release lock for this instance"""
461+ """Release lock for this instance. """
458462 ACQUIRE_LOCK .release_lock ()
459463
460464
461465def is_locked ():
462- """Lock this instance"""
466+ """Lock this instance. """
463467 ACQUIRE_LOCK .is_locked ()
0 commit comments