I am using googletrans lib in python to translate my text. Can someone help me out how do we set proxies in goolgetrans object?
Version of googletrans :3.0.0
I am using googletrans lib in python to translate my text. Can someone help me out how do we set proxies in goolgetrans object?
Version of googletrans :3.0.0
UPDATE: Based on the error described in the comments, you show use the following code. Tested on my environment point to a proxy on cloud:
from googletrans import Translator
from httpcore import SyncHTTPProxy
proxy={"https": SyncHTTPProxy((b'http', b'<proxy server ip>', 3128, b''))}
# http = proxy protocol
# <proxy server ip> = proxy server ip / localhost if its local
# 3128 = proxy server port, change to yours
translator = Translator(service_urls=['translate.googleapis.com'],proxies=proxy)
translation = translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')
print(translation.text)
from googletrans import Translator
from httpcore import SyncHTTPProxy
from base64 import b64encode
def build_proxy_headers(username, password):
userpass = (username.encode("utf-8"), password.encode("utf-8"))
token = b64encode(b":".join(userpass))
return [(b"Proxy-Authorization", b"Basic " + token)]
proxy_url=(b"http", b"<my_proxy_host_ip> ", 3128, b'') # <my_proxy_host_ip> as `bytes`, <my_proxy_port> as `int`
proxy_headers = build_proxy_headers("<username>","<password>" ) # <my_username>, <my_password> as `str`
proxy={"https": SyncHTTPProxy(proxy_url=proxy_url, proxy_headers=proxy_headers) }
translator = Translator(service_urls=['translate.googleapis.com'],proxies=proxy)
translation = translator.translate("Der Himmel ist blau und ich mag Bananen", dest='en')
print(translation.text)
The googletrans uses the httpx library, you can follow its documentation do configure the proxy connection:
send the proxy data in a dictionary variable:
proxies_def = {
"http://": "http://localhost:8030",
"https://": "http://localhost:8031",
}
#Or with authentication:
proxies_def = {
"http://": "http://username:password@localhost:8030",
# ...
}
from googletrans import Translator
translator = Translator(proxies=proxies_def)
Check the httpx documentation here.