Where does getaddrinfo go for name resolution? - python

I have a Raspberry Pi.
ping abc.azurewebsites.net returns the IP address without issue.
However, socket.getaddrinfo('abc.azurewebsites.net',80)
gives:
socket.gaierror: [Errno -5] No address associated with hostname.
To make sure that the error is clear and isolated, I tried the above two commands on another Pi in another network and that one works fine.
How can I resolve this problem?

Related

"Only one usage of each socket address is normally permitted" error when creating new Eel window in python

I am making an application using Eel for python, and it currently is a page with a button that opens up a second page. The first page is using port 8000, and when I open the second page I receive a message due to 8000 being in use. I have copied the message below.
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted: ('localhost', 8000)
I have gotten this message to disappear by changing the port of the second window to 8001. Is this a preferable fix? I plan on having more than 2 windows eventually, so each window would use a different port. I have found similar issues that are fixed by changing the socket option "SO_REUSEADD", but I am not sure how to do this using eel, is it possible? What would be the best way to handle this, could I just ignore it?

<urlopen error [Errno 11001]: getaddrinfo failed>

Could not connect to IP power. Perhaps the selecting of your proxy server in Internet Explorer are incorrect. Sending request to IP Power :http<...>
Need help regarding this issue
I think you have provided an incorrect address to urlopen, otherwise there is a problem with your connection.Check these and if you couldn't resolve it, please update your question with your code.

Get IPs of all devices connected to local network (based on hostname) with python

this is definetly not duplicate.
I am doing an app and I need to find IPs of my devices. I know their name, and through their name I need to get their IP. Linux should be easy but I need cross platform attitude and so therefore I use python.
I already know solution:
import socket
socket.gethostbyname('pc_name')
If I know solution, why I am asking? Well give me moment, because I am not getting stable results.
Situation:
I need to find IPs of several raspberry pi's that has same hostname - lets say:
Raspberry pi device 1: Hostname: 'MyPi', Local IP: 10.0.0.33
Raspberry pi device 2: Hostname: 'MyPi', Local IP: 10.0.0.34
Raspberry pi device 3: Hostname: 'MyPi', Local IP: 10.0.0.35
So according to socket documentation(If I read it correctly),
socket.gethostbyname('MyPi')
returns only one IP, HOWEVER
socket.gethostbyname_ex('MyPi')
should return all IPs for the particular hostname.
I simulated the situation at home and the results were very unsatisfying.
I called my raspberry pi as my Android phone: 'Galaxy-J5-2016'. To make sure I tried to find them by address:
>>> socket.gethostbyaddr('10.0.0.33')
('Galaxy-J5-2016', [], ['10.0.0.33'])
>>> socket.gethostbyaddr('10.0.0.34')
('Galaxy-J5-2016', [], ['10.0.0.34'])
Good , they exist. When I tested it before writing here, I even got one result if I searched
socket.gethostbyname('Galaxy-J5-2016')
BUT now I dont find anything even if I try
socket.gethostbyname_ex('Galaxy-J5-2016')
Both above commands simply wait without response and then respond with this:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
socket.gaierror: [Errno 11001] getaddrinfo failed
Another phenomena occured: When I try lastly mentioned command on my work laptop(Even though my work PC doesnt share hostname with any other computer),
>>> socket.gethostbyname_ex('WORK_PC.DOM1.DOM2')
('WORK_PC.DOM1.DOM2', [], ['172.xx.xx.1', '10.0.xx.1', '172.xx.xx.241',
'192.xx.xx.1', '192.xx.xx.1', **'10.0.0.41'**])
Interestingly 10.0.0.41 is local ip address that my WORK PC has right now at home (The rest seems are from network at my job).
In other words: I have two machines with same Hostname and I get zero or only one IP adress and my work PC is just one MACHINE and i get several IPs of the same machine and these IPs so to say are not even from the actual local network)
PS (In case you will advice me 'arp -a'): general command 'arp -a' is useless as it shows all local IPs that were in recent contact with the command-machine. So I need to ping the particular devices to see them listed in arp -a command.
PS2: Also I dont want any solution that requires going through all IPs. This solution is very slow as pinging IP that doesnt exist will result in several seconds timeout. 255 IP + timeout is not good.
Honestly, If there exist python command to immediatly list all devices connected to Local Network, its enough... But I didnt find anything like this.
Am I doing something wrong? Can you direct me?
Thanks in Advance

socket.gethostname() returning wrong host name

When I was setting up my Macbook Pro I changed my hostname I guess to my name Tristan. I'm trying to work with sockets and call:
socket.gethostbyname(socket.gethostname())
It is not giving me the error socket.gaierror: [Errno 8] nodename nor servname provided, or not known
I'm assuming this is an issue because I have changed the name. If I type:
socket.gethostname('Tristan.local')
It actually prints out the correct ip address. My friend's Mac has not had the hostname changed and it returns the address of the network she is on. How do I change my hostname back to the local hostname or whatever I need to do in order to make:
socket.gethostbyname(socket.gethostname())
Work again? I thought I was being fun at the time by changing it to my name but now I'm full of regret.
gethostname generally just reports what /etc/hostname or equivalent has in it, and that can be just a made up name. gethostbyname tries to resolve that name, and unless it's also in something like /etc/hosts, which some systems set up to match, and it's not resolvable by your configured DNS servers, then you get an error.
So you can try either fixing the name in /etc/hosts if it was there first under the old name, or you can ignore that and just use your host-name as-is.
Remember, unless your public and private IP match that DNS result is useless anyway. What you probably want is to hit a service like Ipify to determine what your actual external IP is.

How to solve Python Sockets/SocketServer Connection [Errno 10048] & [Errno 10049]?

I'm trying to make an online FPS game and so far it works on my local network. What I'm trying to do is make it work globally
I've tried making other Python projects work globally in the past but so far I haven't been able to get it to work. I get my IP from ipchicken or whatever and put it as the HOST for the server, but when I try to start it I get this.
socket.error: [Errno 10049] The requested address is not valid in its context
I've tried many different versions of what could be my IP address found from various different places, but all of them give that output.
I thought, since I had my webspace, I could try doing what it says you can do in the Python manual:
where host is a string representing either a hostname in Internet domain notation like 'daring.cwi.nl'
So, I put in the domain of my webspace (h4rtland.p3dp.com) and I get this error:
socket.error: [Errno 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted
Though only on port 80, anything else gives me the same error as before.
If anybody can shed some light on this subject for me it would be greatly appreciated.
First off, port 80 is typically http traffic. Anything under port 5000 is priviledged which means you really don't want to assign your server to this port unless you absolutely know what you are doing... Following is a simple way to set up a server socket to accept listen...
import socket
host = None #will determine your available interfaces and assign this dynamically
port = 5001 #just choose a number > 5000
for socket_information in socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM):
(family, type, prototype, name, socket_address) = socket_information
sock = socket.socket(family, type, prototype)
sock.bind(socket_address)
max_clients = 1
sock.listen(max_clients)
connection, address = sock.accept()
print 'Client has connected:', address
connection.send('Goodbye!')
connection.close()
This is a TCP connection, for an FPS game you likely want to look into using UDP such that dropped packets don't impact performance terribly... Goodluck

Categories