Skip to content

Commit

Permalink
Merge pull request #81 from sergeybarkov/master
Browse files Browse the repository at this point in the history
Добавлена возможность указывать конкретный прокси в командной строке
  • Loading branch information
rendrom authored Dec 17, 2023
2 parents d0ff8e1 + 5de9df4 commit 737b94c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 3 deletions.
5 changes: 4 additions & 1 deletion rosreestr2coord/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#!/usr/bin/env python
# coding: utf-8
from rosreestr2coord.console import console

import sys,os
sys.path.append(os.getcwd())

from rosreestr2coord.console import console

if __name__ == "__main__":
console()
9 changes: 9 additions & 0 deletions rosreestr2coord/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ def getopts():
required=False,
help="show current version",
)
parser.add_argument(
"-u",
"--proxy_url",
action="store",
type=str,
required=False,
help="set proxy url",
)
opts = parser.parse_args()
return opts

Expand All @@ -142,6 +150,7 @@ def run_console(opt):
"center_only": opt.center_only if opt.center_only else False,
"use_cache": False if opt.refresh else True,
"coord_out": "EPSG:4326",
"proxy_url": opt.proxy_url
}

if opt.list:
Expand Down
4 changes: 4 additions & 0 deletions rosreestr2coord/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ def __init__(
proxy_handler=None,
timeout=5,
logger=logger,
proxy_url=None
):
self.with_log = with_log
self.area_type = area_type
Expand All @@ -108,6 +109,8 @@ def __init__(
self.epsilon = epsilon
self.code = code

self.proxy_url = proxy_url

self.file_name = code_to_filename(self.code[:])
self.with_proxy = with_proxy
self.proxy_handler = proxy_handler
Expand Down Expand Up @@ -226,6 +229,7 @@ def make_request(self, url):
proxy_handler=proxy_handler,
logger=self.logger,
timeout=self.timeout,
proxy_url=self.proxy_url
)
return response

Expand Down
21 changes: 19 additions & 2 deletions rosreestr2coord/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ def get_rosreestr_headers():
proxy_handling = ProxyHandling()


def make_request(url, with_proxy=False, proxy_handler=None, logger=None, timeout=5):
def make_request(url, with_proxy=False, proxy_handler=None, logger=None, timeout=5, proxy_url=None):
if url:
if with_proxy:
if proxy_url is not None:
return make_request_with_specified_proxy(url, proxy_url, logger)
elif with_proxy:
proxy_handler = proxy_handler if proxy_handler else proxy_handling
return make_request_with_proxy(url, proxy_handler, logger, timeout)
try:
Expand All @@ -61,6 +63,21 @@ def make_request(url, with_proxy=False, proxy_handler=None, logger=None, timeout
raise ValueError("The url is not set")


def make_request_with_specified_proxy(url, proxy_url, logger):
attempts = 3
for i in range(1, attempts + 1):
try:
ssl._create_default_https_context = ssl._create_unverified_context
opener = urllib.request.build_opener(
urllib.request.ProxyHandler(
{
'http': proxy_url,
'https': proxy_url
}))
return opener.open(url).read()
except:
logger.debug("Attempt failed, retry")

def make_request_with_proxy(url, url_proxy, logger, timeout):
tries_per_proxy = 3
tries_for_proxies = 20
Expand Down

0 comments on commit 737b94c

Please sign in to comment.