I tried to get external ip & port with Python
I use: pip install pystun
import stun
stun.get_ip_info()
But I got error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.8/dist-packages/stun/__init__.py", line 252, in get_ip_info
nat_type, nat = get_nat_type(s, source_ip, source_port,
File "/usr/local/lib/python3.8/dist-packages/stun/__init__.py", line 186, in get_nat_type
_initialize()
File "/usr/local/lib/python3.8/dist-packages/stun/__init__.py", line 93, in _initialize
dictValToAttr.update({items[i][1]: items[i][0]})
TypeError: 'dict_items' object is not subscriptable
How can I fix that?
That's because pystun
is out of date. pystun
does not support recent version of python. Instead, install pystun3
pip uninstall pystun # uninstall already installed pystun module
pip install pystun3
import stun
print(stun.get_ip_info())
#('Symmetric NAT', '111.111.11.111', 1027)
Or, you can get your external IP address, as follows:
import http.client
conn = http.client.HTTPConnection("ifconfig.me")
conn.request("GET", "/ip")
print(conn.getresponse().read())
For more information, see https://ru.stackoverflow.com/questions/133137/