1515import get_leaf_cert
1616import data_factory
1717
18+
1819class AcquireLock :
1920 """
2021 Create a TCP socket to ensure a single instance.
21-
22- This class creates a TCP socket that binds to a specific port. If an instance
23- of this class is already running, it will log an error and exit.
22+
23+ This class creates a TCP socket that binds to a specific port. If an instance
24+ of this class is already running, it will log an error and exit.
2425
2526 Example usage:
26-
27+
2728 ```python
2829 lock = AcquireLock()
2930 try:
@@ -65,7 +66,6 @@ def __init__(self):
6566 self .misconfigured_server = False
6667 self .leaf_cert = None
6768
68-
6969 def validate_url (self , url ):
7070 """Limit to used domains."""
7171 allowed_domains = (
@@ -80,7 +80,6 @@ def validate_url(self, url):
8080 logging .error ("Invalid URL domain: %s" , url )
8181 sys .exit (1 )
8282
83-
8483 def head (self , curl ):
8584 """Take the curl object to head state, with redirect."""
8685 if isinstance (curl , pycurl .Curl ):
@@ -108,7 +107,6 @@ def head(self, curl):
108107 return buffer .getvalue ().decode ('utf-8' )
109108 return ""
110109
111-
112110 def validate_data_type (self , content_type ):
113111 """Limit to used data types."""
114112 valid_content_types = {
@@ -118,14 +116,13 @@ def validate_data_type(self, content_type):
118116 'image/jpeg' ,
119117 'image/png' ,
120118 'text/html' ,
121- 'head' # this is not MIME
119+ 'head' # this is not MIME
122120 }
123121
124122 if content_type not in valid_content_types :
125123 logging .error ("Invalid content type: %s" , content_type )
126124 sys .exit (1 )
127125
128-
129126 def set_leaf (self , curl ):
130127 """Write the certificate to a temporary file manually"""
131128 if self .leaf_cert is None :
@@ -141,7 +138,6 @@ def set_leaf(self, curl):
141138 curl .setopt (pycurl .CAINFO , temp_cert_path )
142139 return temp_cert_path
143140
144-
145141 def get_leaf (self , url ):
146142 """Check if we need to grab the first certificate"""
147143 if not self .misconfigured_server :
@@ -164,7 +160,6 @@ def setup_before_get_response(self, url, content_type):
164160 if url .startswith ("https://www.trle.net/" ) and not self .misconfigured_server :
165161 self .get_leaf (url )
166162
167-
168163 def get_response (self , url , content_type ):
169164 """Handle all https requests"""
170165 self .setup_before_get_response (url , content_type )
@@ -217,7 +212,7 @@ def get_response(self, url, content_type):
217212 break
218213
219214 except pycurl .error as curl_error :
220- #if curl_error.args[0] == 60: # SSL certificate error
215+ # if curl_error.args[0] == 60: # SSL certificate error
221216 logging .error ("Request failed: %s" , curl_error )
222217 retries += 1
223218 if retries >= max_retries :
@@ -230,7 +225,6 @@ def get_response(self, url, content_type):
230225
231226 return self .close_response (curl , headers , response_buffer , content_type )
232227
233-
234228 def close_response (self , curl , headers , response_buffer , content_type ):
235229 """Pack response and close curl"""
236230 if curl is None :
@@ -251,11 +245,10 @@ def close_response(self, curl, headers, response_buffer, content_type):
251245 response = self .pack_response_buffer (content_type , response_buffer )
252246 curl .close ()
253247 return response
254- logging .error ("Unexpected content type: %s, expected %s" , \
255- response_content_type , content_type )
248+ logging .error ("Unexpected content type: %s, expected %s" ,
249+ response_content_type , content_type )
256250 sys .exit (1 )
257251
258-
259252 def pack_response_buffer (self , content_type , response_buffer ):
260253 """Validate and return the response based on content type"""
261254 if content_type == 'text/html' :
@@ -276,7 +269,6 @@ def pack_response_buffer(self, content_type, response_buffer):
276269 logging .error ("Unsupported content type: %s" , content_type )
277270 return None
278271
279-
280272 def extract_content_type (self , headers ):
281273 """Read the header lines to look for content-type"""
282274
@@ -313,11 +305,12 @@ def progress_callback(self, total_to_download, downloaded, total_to_upload, uplo
313305 if total_to_download > 0 :
314306 if self .progress_bar is None :
315307 # Initialize the progress bar if it's not set
316- self .progress_bar = tqdm (total = total_to_download ,
317- unit = 'B' ,
318- unit_scale = True ,
319- unit_divisor = 1024 ,
320- desc = "Downloading" )
308+ self .progress_bar = tqdm (total = total_to_download ,
309+ unit = 'B' ,
310+ unit_scale = True ,
311+ unit_divisor = 1024 ,
312+ desc = "Downloading" )
313+
321314 self .progress_bar .update (downloaded - self .progress_bar .n ) # Update the progress bar
322315 self .progress_bar .total = total_to_download
323316 return 0 # Returning 0 means to continue
@@ -389,11 +382,12 @@ def download_file(self, url):
389382 curl .setopt (pycurl .WRITEDATA , self .buffer )
390383
391384 # Enable progress meter
392- self .progress_bar = tqdm (total = total_size ,
393- unit = 'B' ,
394- unit_scale = True ,
395- unit_divisor = 1024 ,
396- desc = "Downloading" )
385+ self .progress_bar = tqdm (total = total_size ,
386+ unit = 'B' ,
387+ unit_scale = True ,
388+ unit_divisor = 1024 ,
389+ desc = "Downloading" )
390+
397391 curl .setopt (pycurl .NOPROGRESS , False )
398392 curl .setopt (pycurl .XFERINFOFUNCTION , self .progress_callback )
399393
@@ -435,6 +429,7 @@ def download_file(self, url):
435429REQUEST_HANDLER = RequestHandler ()
436430DOWNLOADER = Downloader ()
437431
432+
438433def get (url , content_type ):
439434 """
440435 Get server response from TRLE or Trcustom hosts
0 commit comments