Ngeheda elichazwe umqondisi we-proxy, i-Requests iveza i-HTTPAdapter.proxy_headers njengendawo yokushintsha:

from urllib.parse import urlsplit
import requests
from requests.adapters import HTTPAdapter

class TaggedProxy(HTTPAdapter):
    def proxy_headers(self, proxy):
        headers = super().proxy_headers(proxy)
        endpoint = urlsplit(proxy)
        if (endpoint.scheme == 'http'
                and endpoint.hostname == 'proxy.example'
                and endpoint.port == 8080):
            headers['X-Proxy-Account'] = 'department-a'
        return headers

with requests.Session() as session:
    adapter = TaggedProxy()
    session.mount('http://', adapter)
    session.mount('https://', adapter)
    response = session.get(
        'https://service.example/data',
        proxies={'https': 'http://proxy.example:8080'},
        timeout=15,
    )

I-X-Proxy-Account iyisibonelo sendawo eqondwa yile proxy ethile, akuyona indlela ejwayelekile yokuqinisekisa kubo bonke abalamuli.

Qala nge-super().proxy_headers(proxy). I-Requests isebenzisa leyo ndlela ukwakha i-Proxy-Authorization lapho i-URL inikeza i-akhawunti ye-Basic. Ukushintsha yonke imiphumela kungalahlekelwa yilokho kuziphatha.

I-adaptha idlulisa la mavelu ku-ProxyManager ye-urllib3. Emhubheni we-HTTPS ahamba ne-CONNECT. Ukuhlelwa kwamaheda esicelo okujwayelekile kuchaza isicelo sakamuva esiya endaweni.

Abaphathi be-proxy bagcinwa ku-cache nge-URL ye-proxy. Ungayithathi le ngwegwe njengokubuyiselwa okungenayo i-cache kusicelo ngasinye sebhizinisi ngenani okufanele lishintshe ekubizeni ngakunye. Khetha ubunikazi bezokuthutha ngokufanele lapho uphawu lwe-akhawunti luhluka.

Ekudlulisweni kwe-HTTP okujwayelekile, izinkambu ze-proxy zisadinga inqubomgomo yokusetshenziswa ehlosiwe umlamuli. Ukuthi iheda likhethekile ku-proxy ku-API yeklayenti akusona isiqinisekiso sokuba yimfihlo uma i-proxy iliyedlulisa.

Izihloko ezihlobene