UlionTse / translators

🌏🌍🌎Translators🌎🌍🌏 is a library that aims to bring free, multiple, enjoyable translations to individuals and students in Python. Translators是一个旨在用Python为个人和学生带来免费、多样、愉快翻译的库。
https://pypi.org/project/translators/
GNU General Public License v3.0
1.62k stars 189 forks source link

[Bug]: translators.server.TranslatorError: Unable to connect the Internet. #110

Closed IveMalfunctioned closed 1 year ago

IveMalfunctioned commented 1 year ago

Debug Tips

What happened?

When attempting to use the library, I get an error telling me it can't connect to the internet. My machine is connected to the internet just fine, so it seems to be an issue with the library.

APP Version

5.5.6

Python Version

3.10

Runtime Environment

Windows 10

Country/Region

United States

Relevant log output

https://gist.github.com/IveMalfunctioned/a320099163faf78d6af138278ca522ef

Screenshots

N/A

Code of Conduct

UlionTse commented 1 year ago

Test the internet please.

translators20230213
IveMalfunctioned commented 1 year ago

Opening python and attempting to import translators results in the same error. https://gist.github.com/IveMalfunctioned/357713f6dce62e880a2b35657b4d1534

UlionTse commented 1 year ago

There are only two reasons. One is your Internet. You can use python to test other web requests, such as requesting any web page to see if they work. The second is to test the link and see if you can get the results properly. The link results indicate which service area your network belongs to.

akynazh commented 1 year ago

Maybe it's about proxy?

\site-packages\translators\server.py

    @property
    def get_server_region(self):
        _headers_fn = lambda url: self.get_headers(url, if_api=False, if_referer_for_host=True)
        try:
            try:
                data = eval(requests.get(self.get_addr_url, headers=_headers_fn(self.get_addr_url)).text[9:-2])
                sys.stderr.write(f'Using state {data.get("stateName")} server backend.\n')
                return data.get('country')
            except requests.exceptions.Timeout:
                ip_address = requests.get(self.get_ip_url, headers=_headers_fn(self.get_ip_url)).json()['origin']
                form_data = {'ip': ip_address, 'accessKey': 'alibaba-inc'}
                data = requests.post(url=self.ip_tb_add_url, data=form_data, headers=_headers_fn(self.ip_tb_add_url)).json().get('data')
                return data.get('country_id')

        except requests.exceptions.ConnectionError:
            raise TranslatorError('Unable to connect the Internet.\n')
        except:
            warnings.warn('Unable to find server backend.\n')
            country = self.default_country or input('Please input your server region need to visit:\neg: [Qatar, China, ...]\n')
            sys.stderr.write(f'Using country {country} server backend.\n')
            return 'CN' if country == 'China' else 'EN'

requests.get() 'proxies' is missing, if i modify to: (my proxy is 127.0.0.1:7890)

data = eval(requests.get(self.get_addr_url, headers=_headers_fn(self.get_addr_url), proxies={'http': '127.0.0.1:7890', 'https': '127.0.0.1:7890'}).text[9:-2])

it works

So I think the problem locates at:

class TranslatorsServer:
    def __init__(self):
        self.server_region = GuestSeverRegion().get_server_region

the method get_server_region could not use proxy

And when I close proxy, the problem is gone.

UlionTse commented 1 year ago

@akynazh requests self adapts to the local proxy environment. So wait for @IveMalfunctioned 's response.

IveMalfunctioned commented 1 year ago

There are only two reasons. One is your Internet. You can use python to test other web requests, such as requesting any web page to see if they work. The second is to test the link and see if you can get the results properly. The link results indicate which service area your network belongs to.

Attempting to connect to this site results in this on my network...

image

however in Tor this is what I get

image

akynazh commented 1 year ago

@akynazh requests self adapts to the local proxy environment. So wait for @IveMalfunctioned 's response.

@UlionTse #26307 fixes wrong parsing of Windows registry proxy settings in 2022.5.12, and I find that some python3.10(like python3.10.4) is before that date. So maybe the problem is here.

My python version is 3.10.9, which is published on 2022.12.6, the bug had been fixed.

akynazh commented 1 year ago

@IveMalfunctioned If you are using proxy, checking whether your python version is after 2022.5.12 may help.

IveMalfunctioned commented 1 year ago

Changing Firefox's proxy settings from 'Use system proxy settings' to 'No proxy' seems to have fixed the link, however my system isn't using a proxy, so that's kinda weird. The problem still persists.

My Python version is 3.11.1 which would be 2022.12.6.

UlionTse commented 1 year ago

@IveMalfunctioned Computer network addresses have apparently been tampered with or overprotected by the use of tor. The existence of tor itself and access to the network address itself is the relationship between the spear and shield.

IveMalfunctioned commented 1 year ago

I know, I was just showing that it at least connected to the site with Tor, and on my browser it didn't connect at all.

UlionTse commented 1 year ago

@IveMalfunctioned Proxy or tor affects the network, which means that it affects your entire computer network, not just a browser. If you borrow a computer or mobile phone that has never used proxy or torto open that page, you should have no problem now.

IveMalfunctioned commented 1 year ago

Yeah I know, I was showing you that my internet wasn't working with the site, so it was either my network or computer.

UlionTse commented 1 year ago

@IveMalfunctioned Please upgrade version to 5.6.0 Do this os.environ['translators_default_region'] = 'xxx' before import translators, and then we'll skip the previous question.

IveMalfunctioned commented 1 year ago

Problem was fixed by reinstalling in command prompt: pip install --force-reinstall translators=5.6.3 (or latest) and fixing updated syntax

SeanDunford commented 11 months ago

@UlionTse can you explain a bit more as to why the following line:

os.environ['translators_default_region'] = 'xxx'

fixes the issue?

I'm using translators==5.7.6 on a mac and i was getting the unable to connect to the internet issue. The following link: https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location does not load in any browser from my mac. Not using any non-default networking firewall/proxy on my machine.

@IveMalfunctioned Please upgrade version to 5.6.0 Do this os.environ['translators_default_region'] = 'xxx' before import translators, and then we'll skip the previous question.

UlionTse commented 11 months ago

@SeanDunford

Hello, bro. The essence of this function is to show the location of the translation service used by the user, because different regions, even if the domain name is the same, the service provider may have different services. Second, to differentiate services in China from those in other countries, because Google and Microsoft offer additional services in China.

Based on that, can you load this https://httpbin.org/ip?

UlionTse commented 11 months ago

@UlionTse can you explain a bit more as to why the following line:

os.environ['translators_default_region'] = 'xxx'

fixes the issue?

I'm using translators==5.7.6 on a mac and i was getting the unable to connect to the internet issue. The following link: https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location does not load in any browser from my mac. Not using any non-default networking firewall/proxy on my machine.

@IveMalfunctioned Please upgrade version to 5.6.0 Do this os.environ['translators_default_region'] = 'xxx' before import translators, and then we'll skip the previous question.

@SeanDunford os.environ['translators_default_region'] = 'xxx' skips IP identification. Which country or region are you in? I would like to know which region does not have access to this network address, thanks.

SeanDunford commented 11 months ago

@UlionTse can you explain a bit more as to why the following line:


os.environ['translators_default_region'] = 'xxx'

fixes the issue?

I'm using translators==5.7.6 on a mac and i was getting the unable to connect to the internet issue. The following link: https://geolocation.onetrust.com/cookieconsentpub/v1/geo/location does not load in any browser from my mac. Not using any non-default networking firewall/proxy on my machine.

@IveMalfunctioned Please upgrade version to 5.6.0 Do this os.environ['translators_default_region'] = 'xxx' before import translators, and then we'll skip the previous question.

@SeanDunford os.environ['translators_default_region'] = 'xxx' skips IP identification. Which country or region are you in? I would like to know which region does not have access to this network address, thanks.

In nyc and the ip lookup site does work.

UlionTse commented 11 months ago

@SeanDunford Bro, please pip install --upgrade translators==5.8.6.