@sergeM93

Как подключиться к заблокированному серверу через прокси?

(API call)

import os
import pandas as pd
import motuclient
import datetime as dt
from datetime import datetime, date, timedelta

tm =dt.datetime.today()
sm =tm + timedelta(days=3)
START_TIME = str(tm.year) + '-' + '%02d' % tm.month + '-' + '%02d' % tm.day + '00:00:00'
END_TIME = str(sm.year) + '-' + '%02d' % sm.month + '-' + '%02d' % sm.day + '00:00:00'

class MotuOptions:
    def __init__(self, attrs: dict):
        super(MotuOptions, self).__setattr__("attrs", attrs)

    def __setattr__(self, k, v):
        self.attrs[k] = v

    def __getattr__(self, k):
        try:
            return self.attrs[k]
        except KeyError:
            return None

__author__ = "Copernicus Marine User Support Team"
__copyright__ = "(C) 2021 E.U. Copernicus Marine Service Information"
__credits__ = ["E.U. Copernicus Marine Service Information"]
__license__ = "MIT License - You must cite this source"
__version__ = "202105"
__maintainer__ = "D. Bazin, E. DiMedio, J. Cedillovalcarce, C. Giordan"
__email__ = "servicedesk dot cmems at mercator hyphen ocean dot eu"

def motu_option_parser(script_template, st, et, usr, pwd, output_filename):
    dictionary = dict(
        [e.strip().partition(" ")[::2] for e in script_template.split('--')])
    dictionary['variable'] = [value for (var, value) in [e.strip().partition(" ")[::2] for e in script_template.split('--')] if var == 'variable']  # pylint: disable=line-too-long
    for k, v in list(dictionary.items()):
        if v == '<OUTPUT_DIRECTORY>':
            dictionary[k] = '.'
        if v == '<OUTPUT_FILENAME>':
            dictionary[k] = output_filename
        if v == '<START_TIME>':
            dictionary[k] = st
        if v == '<END_TIME>':
            dictionary[k] = et
        if v == '<USERNAME>':
            dictionary[k] = usr
        if v == '<PASSWORD>':
            dictionary[k] = pwd
        if k in ['longitude-min', 'longitude-max', 'latitude-min',
                    'latitude-max', 'depth-min', 'depth-max']:
            dictionary[k] = float(v)
            dictionary[k.replace('-','_')] = dictionary.pop(k)
            dictionary.pop('python')
            dictionary['auth_mode'] = 'cas'
    return dictionary

frcst_regist = r"\\data.fertoing.ru\data\0007-ОМОС\Реестр_информации\Графики_ГРОМ\2023\Реестр_прогнозов_WW.xlsx"
core = r'\\data.fertoing.ru\data\0007-ОМОС\П.0101.41.8_Гидромет\2023\111_Фертоинг\Гидрометобеспечение\\'
Df = pd.read_excel(frcst_regist)

USERNAME = 'ggrom'
PASSWORD = 'Sa123456'
active = Df[(Df.Active == 'yes') &
            (Df['Dwnld Time'] == tm.hour)].rename(columns={'Project': 'code',
                                                            'Dwnld': 'dwnld',
                                                            'Dwnld Time': 'dwnld_tim',
                                                            'Service id': 'service_id',
                                                            'Product id': 'product_id',
                                                            'Coord System': 'coord_system',
                                                            'Lat S': 'S',
                                                            'Lat N': 'N',
                                                            'Lon W': 'W',
                                                            'Lon E': 'E'})
for i in active.index:
    Code = active.loc[i].code
    Dwnld = active.loc[i].dwnld
    if Dwnld == 'yes':
        script_template = \
            "python -m motuclient --motu https://nrt.cmems-du.eu/motu-web/Motu --service-id " + \
            active.loc[i].service_id + \
            " --product-id " + active.loc[i].product_id + \
            " --longitude-min " + str(active.loc[i].W) + " --longitude-max " + str(active.loc[i].E) + \
            " --latitude-min " + str(active.loc[i].S) + " --latitude-max " + str(active.loc[i].N) + \
            " --date-min <START_TIME> --date-max <END_TIME>"
        if active.loc[i].VHM0 == 'ok':
            script_template += " --variable VHM0 "
        if active.loc[i].VHM0_sw1 == 'ok':
            script_template += "--variable VHM0_SW1 "
        if active.loc[i].VHM0_ww == 'ok':
            script_template += "--variable VHM0_WW "
        if active.loc[i].VMDR == 'ok':
            script_template += "--variable VMDR "
        if active.loc[i].VMDR_sw1 == 'ok':
            script_template += "--variable VMDR_SW1 "
        if active.loc[i].VMDR_ww == 'ok':
            script_template += "--variable VMDR_WW "
        if active.loc[i].VZMX == 'ok':
            script_template += "--variable VZMX "
        if active.loc[i].VTPK == 'ok':
            script_template += "--variable VTPK "
        script_template += "--out-dir <OUTPUT_DIRECTORY> --out-name <OUTPUT_FILENAME> " + \
                            "--user <USERNAME> --pwd <PASSWORD>"
        print('Script template: ' + script_template)

        path = core + Code + '\\FRCST\\' + date.today().strftime("%Y%m%d") + '\\'
        if not os.path.exists(path):
            os.makedirs(path)
        OUTPUT_FILENAME = path + 'WW_RAW_' + Code + '_' + "%02d" % tm.day  + "%02d" % tm.month + str(tm.year) + '.nc'
        data_request_options_dict_automated = motu_option_parser(script_template, START_TIME, END_TIME, USERNAME, PASSWORD, OUTPUT_FILENAME)
        print('Data request: ' + str(data_request_options_dict_automated))

        try:
            motuclient.motu_api.execute_request(MotuOptions(data_request_options_dict_automated))
            print('Wave data ' + Code + ' are downloaded')
        except:
            print('Wave data ' + Code + ' are not downloaded')


(Proxy)

from seleniumwire import undetected_chromedriver as uc
from selenium import webdriver

def browser_with_proxy():
    wire_options = {
        'proxy': {
            'https': f'https://yM21Y8bM:5EdjTQaS@107.187.35.3:64684',
        }
    }
    options = webdriver.ChromeOptions()
    options.add_argument('--ignore-ssl-errors=yes')
    options.add_argument('--ignore-certificate-errors')
    driver = uc.Chrome(version_main=118, seleniumwire_options=wire_options, options=options)
browser_with_proxy()


Задача состоит в подключении к заблокированному серверу через прокси, есть 2 блока кода: 1) API Call и 2) Proxy. Сейчас происходит блокировка на стадии запроса и сервер не отвечает, подскажите в какую часть API нужно вставить блок кода с Proxy, чтобы успешно подключиться к серверу и выкачать нужные данные?
  • Вопрос задан
  • 63 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы