Python-for-android: socket.getaddrinfo appears to be completely broken, all name resolutions fail

Created on 7 Nov 2018  路  5Comments  路  Source: kivy/python-for-android

Versions

  • Python: python3crystax (app), python 3 on host for building
  • OS: Ubuntu in docker for building, android 7 for test
  • Kivy: not used
  • Cython: most likely not relevant (can check if it is)

Description

Name resolution with socket.getaddrinfo just seems to plain not work, here is what I get for google.com (while nslookup google.com in termux or just browsing google.com in the web browser works fine):

11-07 03:17:28.760 30786 30818 I python  : Traceback (most recent call last):
11-07 03:17:28.760 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/site-packages/nettools/__init__.py", line 59, in dns_lookup_multitarget
11-07 03:17:28.761 30786 30818 I python  :     name, None, socket.AF_INET6)
11-07 03:17:28.761 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/stdlib.zip/socket.py", line 743, in getaddrinfo
11-07 03:17:28.761 30786 30818 I python  :     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
11-07 03:17:28.761 30786 30818 I python  : socket.gaierror: [Errno 7] No address associated with hostname
11-07 03:17:28.761 30786 30818 I python  : 
11-07 03:17:28.761 30786 30818 I python  : During handling of the above exception, another exception occurred:
11-07 03:17:28.761 30786 30818 I python  : 
11-07 03:17:28.761 30786 30818 I python  : Traceback (most recent call last):
11-07 03:17:28.761 30786 30818 I python  :   File "<console>", line 1, in <module>
11-07 03:17:28.761 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/site-packages/nettools/__init__.py", line 78, in dns_lookup
11-07 03:17:28.761 30786 30818 I python  :     raise last_error
11-07 03:17:28.762 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/site-packages/nettools/__init__.py", line 66, in dns_lookup_multitarget
11-07 03:17:28.762 30786 30818 I python  :     name, None, socket.AF_INET)
11-07 03:17:28.762 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/stdlib.zip/socket.py", line 743, in getaddrinfo
11-07 03:17:28.762 30786 30818 I python  :     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
11-07 03:17:28.762 30786 30818 I python  : socket.gaierror: [Errno 7] No address associated with hostname

The same code works fine on desktop (with python 3 on GNU/Linux and Microsoft Windows). Please note I wrote nettools so I'm not using some odd third-party wrapper that may do whatever instead of just looking up the hostname - all the function I wrote does is automatically try AF_INET6 first and fall back to AF_INET if that doesn't work (as should be easy to reconstruct from the backtrace).

I tested this on latest master with the ctypes fix pull request on top ( #1433 ) which shouldn't affect name resolution.

buildozer.spec

not used, I build with a p4a one liner

Logs

see backtrace above

Most helpful comment

:laughing: yup, was internet permission

All 5 comments

I forgot I have an interactive debug console in my app, where I tried this:

import socket; socket.getaddrinfo("google.com", None, socket.AF_INET)

This is the result:

11-07 03:26:16.767 30786 30818 I python  : Traceback (most recent call last):
11-07 03:26:16.767 30786 30818 I python  :   File "<console>", line 1, in <module>
11-07 03:26:16.767 30786 30818 I python  :   File "/mnt/expand/50b713c2-bef0-4226-aea1-000678d55d07/user/0/com.example.app/files/app/crystax_python/stdlib.zip/socket.py", line 743, in getaddrinfo
11-07 03:26:16.768 30786 30818 I python  :     for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
11-07 03:26:16.768 30786 30818 I python  : socket.gaierror: [Errno 7] No address associated with hostname

WIth my desktop python, it works (try for yourself if you're curious):

$ python3
Python 3.6.6 (default, Jul 19 2018, 14:25:17) 
[GCC 8.1.1 20180712 (Red Hat 8.1.1-5)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket; socket.getaddrinfo("google.com", None, socket.AF_INET)
[(<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_STREAM: 1>, 6, '', ('172.217.22.14', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_DGRAM: 2>, 17, '', ('172.217.22.14', 0)), (<AddressFamily.AF_INET: 2>, <SocketKind.SOCK_RAW: 3>, 0, '', ('172.217.22.14', 0))]

Edit: I just tested, socket.gethostbyname gives the same error. How does name resolution work for any of you guys on Android?? Wondering if I'm doing something very obviously wrong...

Is there anyone for whom socket.gesthostbyname works with latest p4a master and python3crystax? I'm kind of poking in the dark here

Well, I assumed since Android never prompts for an "internet" permission in recent versions that it wasn't needed :rofl: so yeah... still busy building a new test version but I assume that's the culprit. (Will close tickets in a few minutes once I confirmed with the new test build)

:laughing: yup, was internet permission

how to give android internet permissions(I gave it, but it does not show up)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alex-at-thimble picture alex-at-thimble  路  6Comments

ccharoux picture ccharoux  路  5Comments

etc0de picture etc0de  路  3Comments

KeyWeeUsr picture KeyWeeUsr  路  4Comments

excepterror picture excepterror  路  9Comments