Requests ના HTTPAdapter.proxy_headers સાથે પ્રોક્સી હેડર સેટ કરવા
પ્રોક્સી દ્વારા નિર્ધારિત ફીલ્ડ માટે HTTPAdapter.proxy_headers વિસ્તારો, Requests ની હાલની પ્રોક્સી-પ્રમાણીકરણ હેડર રચના જાળવી રાખીને.
પ્રોક્સી ઓપરેટર દ્વારા નિર્ધારિત હેડર માટે Requests HTTPAdapter.proxy_headers ને ઓવરરાઇડ બિંદુ તરીકે આપે છે:
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,
)
X-Proxy-Account એ આ ચોક્કસ પ્રોક્સી સમજે તેવા ફીલ્ડનું ઉદાહરણ છે, દરેક મધ્યસ્થ સાથે પ્રમાણીકરણની પ્રમાણભૂત રીત નથી.
super().proxy_headers(proxy) થી શરૂ કરો. URL Basic ખાતું આપે ત્યારે Requests તે પદ્ધતિ વાપરીને Proxy-Authorization બનાવે છે. આખું પરિણામ બદલવાથી તે વર્તન ગુમાવી શકાય છે.
એડેપ્ટર આ મૂલ્યોને urllib3 ના ProxyManager માં મોકલે છે. HTTPS ટનલમાં તે CONNECT સાથે જાય છે. સામાન્ય વિનંતી-હેડર મેપિંગ પછીની ગંતવ્ય-વિનંતી વર્ણવે છે.
પ્રોક્સી મેનેજર પ્રોક્સી URL પ્રમાણે કેશ થાય છે. દરેક કૉલમાં બદલવાની હોય તેવી કિંમત માટે આ હૂકને કેશ-રહિત પ્રતિ-વ્યાવસાયિક-વિનંતી કૉલબેક ગણશો નહીં. ખાતા માર્કર બદલાય ત્યારે તે મુજબ ટ્રાન્સપોર્ટ માલિકી પસંદ કરો.
સાદા HTTP ફોરવર્ડિંગમાં પ્રોક્સી ફીલ્ડને હજી પણ મધ્યસ્થના ઇચ્છિત વપરાશ ધોરણની જરૂર છે. ક્લાયન્ટ API માં હેડર પ્રોક્સી-વિશિષ્ટ હોવું ગોપનીયતાની બાંયધરી નથી, જો પ્રોક્સી તેને આગળ મોકલે.