Yí URL aṣojú HTTP padà sí ipari Twisted
Pín URL aṣojú HTTP aṣa kan sí olúwa àti ibudo dípò kí o kọjá rẹ̀ gẹ́gẹ́ bí gírámà àpèjú ipari Twisted.
Iye aṣojú aṣa bii http://proxy.example:8080 jẹ́ URL. clientFromString ti Twisted gba àpèjú ipari, bii tcp:host=proxy.example:port=8080. Àwọn gírámà méjèèjì ń ṣàpèjú ìsọfúnni tó ní ìbáṣepọ̀ pẹ̀lú gírámà tó yàtọ̀.
Bí ìpèlò náà bá gba URL aṣojú HTTP aṣa, pín ú ṣáájú kí o tó dá ipari náà:
import os
from urllib.parse import urlsplit
from twisted.internet.endpoints import HostnameEndpoint
from twisted.web.client import ProxyAgent
proxy = urlsplit(os.environ['http_proxy'])
if proxy.scheme != 'http' or not proxy.hostname:
raise ValueError('Expected an HTTP proxy URL')
port = proxy.port if proxy.port is not None else 80
endpoint = HostnameEndpoint(reactor, proxy.hostname, port, timeout=5)
agent = ProxyAgent(endpoint, reactor=reactor)
Àpín yìí gbà pé ìpèlò náà ti ní reactor rẹ̀. Sọ ibudo aṣojú gidi nínú ìṣètò nígbà tí kì í ṣe àìsí URL HTTP. Àkójọ aṣojú HTTP ń pèsè àwọn olùdíje ipari fún ìbéèrè HTTP gbangba tí a gbà láyè láìsí àkọsílẹ̀ aṣojú. HostnameEndpoint tún yẹra fún fífi orúkọ olúwa sínú gírámà ìṣètò mìíràn tí a fi kólónì pín.
Èyí ń yí ìwọlé ìtọ́sọ́nà nìkan padà. Àwọn ẹ̀rí tí a fi sínú URL kì í di àkọlé àṣẹ nìkan nítorí pé a pín olúwa àti ibudo. Pèsè ìjẹ́rìí aṣojú tí a ṣe àtìlẹ́yìn tí ó yẹ nípasẹ̀ ètò ìjẹ́rìí ìbéèrè náà. Fún ìdí HTTP tó rọrùn àti alárinà tí ń pèsè Basic, àwọn àkọlé ìbéèrè Basic ti ProxyAgent ń pèsè irú ètò bẹ́ẹ̀.
URL aṣojú HTTPS nílò ìsopọ̀ tó lè TLS sí aṣojú náà. URL SOCKS nílò ìjíròrò SOCKS. Kò sí èyí tí a ń ṣe nípasẹ̀ fífi àwọn ètò yẹn ránṣẹ́ sínú ìyípadà ipari HTTP ProxyAgent tó rọrùn.